- Laravel Version: 9.42.0
- PHP Version: 8.1.13
- Database Driver & Version:
Description:
Before laravel 9 update, form rules were working as I wanted. It started behaving differently after the update.
While the same code and the same request occur without an error in laravel 8, error code 422 is returned in laravel 9.
Steps To Reproduce:
Route::post('test', function (\Illuminate\Http\Request $request) {
$request->validate([
'type' => [
'bail',
'exclude_if:type,',
'in:individual,corporate',
],
'name' => [
'bail',
'exclude_if:type,',
'required_with:type',
'min:3',
'max:49',
],
'id' => [
'bail',
'exclude_if:type,',
'required_with:type',
'digits:10',
],
'tax_office' => [
'bail',
'exclude_unless:type,corporate',
'required_if:type,corporate',
'min:2',
'max:99',
],
], $request->all());
});
Request:
curl --location --request POST 'https://site.dev/api/test' \
--header 'Accept: application/json' \
--header 'X-Requested-With: XMLHttpRequest' \
--header 'Content-Type: application/json;charset=UTF-8' \
--form 'type=""' \
--form 'name=""' \
--form 'id=""' \
--form 'tax_office=""'
Laravel 8.21.0 response status code: 200
Laravel 9.42.0 response status code: 422
CodePudding user response:
Error code 422 with validation in Laravel means the request didn't pass the validation. You would have to debug it further to see which field in the payload is causing the issue by inspecting response errors.
If you got code 200 in a previous version, either you are providing it with different payload which was valid, or something changed between the two versions with the types of rules you are using.
If I had to guess I would say the problem is with the payload data you are providing, and likely before when you got code 200, you were testing with different payload or alternatively maybe you weren't using CURL to test before but instead through the browser.
You can check here to see if any of these upgrades may have had an impact: https://laravel.com/docs/9.x/upgrade
CodePudding user response:
Laravel sends HTTP 422
upon a validation failure ... but, which field failed validation? And, what was the message? (check the response body).
HTTP 200
means the request succeeded — since you don't specify a return or return response()
in your controller, HTTP 200
is the default response.
Your usage of 'exclude_if required_with'
seems a bit weird ... perhaps in Laravel updated the logic to fix a bug? (You might try just using required
vs required_with
).
I'd also try loading that request (import the CURL) into some IDE to help with debugging — Postman How to import