Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sedattcom's avatar

Laravel Form Validation Inconsistency

  • 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 'url/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

0 likes
1 reply
krs's avatar

Hi, what error message comes with your 422 response? If the validation fails, Laravel should tell you.

Perhaps you have to remove the second param in the validate-method (the $request->all()). The second param are the options like custom error messages.

Please or to participate in this conversation.