Why doesn't a custom rule activeForm?

There is a form with a field (there are other fields, but the theme does not apply):
<?=$form->field($model,'date', ['enableAjaxValidation' => true, 'enableClientValidation' => false])->input('date');?>

and there is a test custom rule that, 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', 'Value: '.$this->$attribute);
 }

If you change the ajax works, it appears the mistakes of other fields, but not the error of date field.
What could be the problem?
April 2nd 20 at 17:23
1 answer
April 2nd 20 at 17:25
Solution
In the action controller add
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
 Yii::$app->response->format = Response::FORMAT_JSON;
 return ActiveForm::validate($model);
 }

Although the ajax validate better to do a single action, even a separate controller.
Here there is shown what is what.
It's already there:
if(\Yii::$app->request->isAjax){ // ajax validation
\Yii::$app->response->format=Response::FORMAT_JSON;
 return ActiveForm::validate($model);
 }

I'm telling you, the error array in the ajax comes in, but there is no error of custom rules. - Raymundo.Schmeler commented on April 2nd 20 at 17:28
@Raymundo.Schmeler, You write back something, then it will be the rule. - amya.Ry commented on April 2nd 20 at 17:31
@Raymundo.Schmeler, I checked Your last code. Everything is working fine. - amya.Ry commented on April 2nd 20 at 17:34
{"activity-title":["you Must fill in "Title"."],"activity-timestart":["Need to fill "start Time"."],"activity-timefinish":["you Must fill in the "end Time"."]}
- Raymundo.Schmeler commented on April 2nd 20 at 17:37
@Raymundo.Schmeler, everything works))) You're doing it wrong.
In the form
$form->field($model, 'email', ['enableClientValidation' => false, 'enableAjaxValidation' => true])

In the model
public function rules()
{
 return [
 // name, email, subject and body are required
 [['name', 'email', 'subject', 'body'], 'required'],
 // email has to be a valid email address
 ['email', 'checkDate'],
 // verifyCode needs to be entered correctly
 ['verifyCode', 'captcha'],
];
}

 public function checkDate($attribute, $param) {
 $this->addError('email', 'Value: check the operation of the code for the question from @Raymundo.Schmeler '.$this->$attribute);
 }

In the controller
public function actionContact()
{
 $model = new ContactForm();

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

 if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');

 return $this->refresh();
}
 return $this->render('contact', [
 'model' => $model,
]);
 }

The result
5de7885aee547824064700.gif

The problem is, most likely, You have in this line.
if(\Yii::$app->request->isAjax){ // ajax validation
You do not download the data from the POST request.
Do I have
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
- amya.Ry commented on April 2nd 20 at 17:40

Find more questions by tags Yii