H
H
hollanditkzn2017-09-05 10:04:44
Yii
hollanditkzn, 2017-09-05 10:04:44

Why does it always output null for input?

I solved the issue by doing this by default in the model so that the user id is displayed, but for the sake of interest, for the first time I encountered such a problem that it constantly displays null for me. even if required still fails validation.

class Zakaz extends ActiveRecord
{
const SCENARIO_DEFAULT  = 'default';

public function scenarios()
    {
        return [
            self::SCENARIO_DECLINED => ['declined', 'required'],
            self::SCENARIO_DEFAULT => ['id_shop', ...],
        ];
public function rules()
    {
        return [
            [['srok', 'number', 'description', 'phone', 'id_client', 'id_shop'], 'required', 'on' => self::SCENARIO_DEFAULT],
            ['declined', 'required', 'message' => 'Введите причину отказа', 'on'=> self::SCENARIO_DECLINED],
            [['id_shop', ...], 'integer'],
//            ['id_shop', 'default', 'value' => Yii::$app->user->getId()],
           ...
        ];
    }
    }

And in the view
<?= $form->field($model, 'id_shop')->textInput(['value' => Yii::$app->user->id]) ?>

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Timofeev, 2017-09-05
@webinar

Why does it always output null for input

what are you speaking about? Is your value null?
judging by the code, value gets into Yii::$app->user->idand it is null, there is a user not registered. If there is no user, what id do you expect to see?
There is another important question, when is it null? For there is a form, then it is sent, validated, saved and displayed again. At what point is null and why do you expect it to be otherwise?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question