M
M
Maxim Lagoysky2017-10-12 11:36:32
Yii
Maxim Lagoysky, 2017-10-12 11:36:32

Why is the model not receiving data from some fields?

There is a model, it has optional fields that are loaded by a separate view after certain actions. Validation for loaded fields works, but when submitting the form, there is no data for the loaded field in the model.
59df26fe7049c112646215.jpeg
At the same time, there is no size field in attributes.
59df297faa41f383825754.jpeg
The controller itself is nothing interesting here

public function actionTest(){
        $model = new PostFormTest();
        $category = Category::find()->all();
        $city = City::find()->all();

        if($model->load(Yii::$app->request->post())){
            if($model->validate()){
                $this->debug($model);
            }
        }
        return $this->render('test', compact('model', 'category', 'city'));
    }

And the loading form
<? $form = ActiveForm::begin([
    'options' => ['enctype' => 'multipart/form-data', 'id' => 'sub_category_1'],
])?>
    <div class="row">
        <?= $form->field($model,'size',['options' => ['class' => 'input-field col s12 m4'], 'template' =>
            '{input}<label class="control-label" for="post-title">Size<span class="req"> *</span></label>{error}'])->input('number') ?>
    </div>
<? $form = ActiveForm::end() ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Lagoysky, 2017-10-12
@lagoy

There is a view in the form there is a form, there is a select in the form, when a certain option is selected, an ajax request is sent to the controller, in this controller, depending on the data received, an additional view is called in which the Size field is displayed through ActiveForm. This field is validated according to the rules described in the model, but when you click the "send" button, data comes only from the very first model, that is, data from the view that was rendered using ajax does not come.

I have already described in as much detail as possible ... above there is everything you need with screenshots

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question