A
A
Aricus2019-12-03 09:41:48
Yii
Aricus, 2019-12-03 09:41:48

Why doesn't the activeForm custom rule work?

There is a form with a field (there are other fields, but it does not apply to the topic):

<?=$form->field($model,'date', ['enableAjaxValidation' => true, 'enableClientValidation' => false])->input('date');?>

and there is a test custom rule, which, in theory, should always give an error.
public function rules()
    {
        return [
            ['date', 'date', 'format' => 'php:Y-m-d'],
            ['date', 'checkDate'],
        ];
    }

    public function checkDate($attribute, $param) {
        $this->addError('date', 'Значение: '.$this->$attribute);
    }

When the ajax field changes, it works, errors of other fields appear in it, but the error of the date field does not appear.
What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2019-12-03
@Aricus

In the controller action add

if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }

Although for ajax validation it is better to do a separate action, even a separate controller.
Here it is shown what's what.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question