yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework

Home Page:http://www.yiiframework.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AjaxValidation: ajax response with validation errors is not processed in 2.0.47 - 2.0.49.1

umyskov opened this issue · comments

What steps will reproduce the problem?

Install Yii 2.0.49.1, make form with ajax validation, fill fields incorrectly, push submit button/

What is the expected result?

After submit, the ajax request is sent then returned with a json response containing validation errors, the form shows validation errors, form not sent and submit button is blocked.

What do you get instead?

After submit, the ajax request is sent then returned with a json response containing validation errors, the errors not displayed and the form is sent anyway.

Additional info

Q A
Yii version 2.0.49.1
PHP version 7.4
Operating system Ubuntu

2.0.46 is alright?

yes. 2.0.46 is alright

Is it reproduceable with regular form in a basic app template?

I reproduced it on an advanced one, but maybe in this case it doesn't matter where to reproduce it

A Closer look at this issue, it seems OP missed below part in the documentation. Here is the part in question:

Note: When the enableAjaxValidation property is configured at both the input field level and the form level, the former will take precedence.

So in the controller you need to add the code proposed, that is:

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

I will close it for now. @umyskov, feel free to reopen if the issue persists after applying the above code.