Empty value is being saved in db

Hi, I have some problems with saving form data. Can someone help me? Here some stroke of my code.




public function actionStart() {

        $model = new Start();




        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $model->setData();

        }


        return $this->render('start', [

            'model' => $model,

        ]);

    }

from model


public function setData() {

        $user = new Start();

        $user->title = $this->title;

        $user->description = $this->description;

        $user->save();

    }


from view


<div class="col-lg-5">

                <?php $form = ActiveForm::begin(['id' => 'Start']); ?>

                <?= $form->field($model, 'title') ?>

                <?= $form->field($model, 'description') ?>

                <div class="form-group">

                    <?= Html::submitButton('Start', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>

                </div>

                <?php ActiveForm::end(); ?>

            </div>



I getting data form. If I show $user->title, $user->description in model before save I see form data but in db I have record with empty values(

Thank u for a help.

My guess is it’s going wrong in a couple of places, especially on the model. Also don’t typically see the name of the action as the name of the model. You would be better off generating your code from Gii, which will show you the recommended structure. Basically your start action is a create action and Gii gives you a nice concise format for this in the controller:





public function actionCreate()

    {

        $model = new Faq();


        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            return $this->redirect(['view', 'id' => $model->id]);

        } else {

            return $this->render('create', [

                'model' => $model,

            ]);

        }

    }



The code snippet is for my Faq model, but all of the code is from Gii. You can see the save method is called with post (which also validates), so a lot is done for you. If there is no data to post or validation fails, it shows the create form, which is the create view.

If the save is successful, the action redirects you to the view for the instance of the model you just created. You can change that if you like.

If you build your code with Gii, you will get a good idea of the intended structure. You will have to do a little refining, mostly to the rules method on the model, which is the validation rules, and to _form.php, which is a partial view that gets rendered inside of create.php. But you don’t even need to id the form, it knows where to post if you have followed Yii 2’s format exactly.

You don’t have to call the action create, however, if you are just learning the framework, it’s a good idea to stick with their conventions, it’s far less confusing in the long run. Also, you can read further in the guide on this topic. I hope this helps answer your question.

Thanks, I’ll try.

In your model don’t forget about setting up the rules:





class <YourClass> extends Model {

...

    public $title;

    public $description;


    public function rules()

    {

        return [

            [['title', 'description'], 'required'] // if fields are required

            // [['title', 'description'], 'safe'] // <- un-comment this line if fields are not required

        ];

    }

...

}