yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

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

Open umyskov opened this issue 2 years ago • 4 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

umyskov avatar Dec 18 '23 12:12 umyskov

2.0.46 is alright?

samdark avatar Dec 20 '23 11:12 samdark

yes. 2.0.46 is alright

umyskov avatar Dec 21 '23 15:12 umyskov

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

samdark avatar Dec 21 '23 17:12 samdark

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

umyskov avatar Dec 25 '23 13:12 umyskov

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.

mtangoo avatar May 29 '24 10:05 mtangoo