V
V
Veronika Stepanovna2016-10-29 17:15:58
Yii
Veronika Stepanovna, 2016-10-29 17:15:58

Why is the form data not being validated?

There is a form

<?=$form->field($model, '_author')->label('Имя')?>
<?=$form->field($model, '_email')->label('Email')?>
<?=$form->field($model, '_comment')->label('Комментарий')->textArea(['rows' => 3])?>
    <div class="form-group">
        <div class="col-lg-offset-0 col-lg-1">
            <?= Html::submitButton('Update', ['class' => 'btn btn-primary']) ?>
        </div>
    </div>
<?php ActiveForm::end() ?>

And handler
public function editComment($id)
{
    $comment = Comments::findOne($id);
    $comment->author = $this->_author;
    $comment->comment = $this->_comment;
    $comment->email = $this->_email;
    return $comment->save();
}

Validation rules
public function rules()
{
    return [
        [['_author', '_comment', '_email'], 'required'],
        [['_author', '_comment'], 'string'],
        ['_email', 'email']
    ];
}

When updating data with validation, the update does not occur. The method $model->validate()returns falsealthough the data is valid. Have to use $comment->save(false). I can't figure out what could be the problem?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Timofeev, 2016-10-31
@webinar

To see validation errors, it is enough to output
after $comment->save()
Why guess on coffee grounds?

D
Dmitry, 2016-10-29
@slo_nik

Good afternoon.
Open any controller generated by crud and see how the record update is implemented there.

// тут получаете модель из метода findModel()
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
   // перенаправление куда Вам надо
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question