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

gazd1977's avatar

422 (Unprocessable Entity) Error on ajax request

Hi all,

I'm trying to work through an example of form validation using the spatie / form-backend-validation library.

Im posting to an api url where the controller simply validates the request

  $validatedData = $request->validate([
            'address1' => 'required|string|max:255',
            'address2' => 'string|max:255',
            'city' => 'string|max:255',
            'county' => 'string|max:255',
            'country' => 'string|max:255',
            'zip' => 'required|string|max:255'
        ]);

At this stage im only console logging whether there are errors or not

this.form.post('/api/address')
       .then(response => console.log('success'))
       .catch(response => console.log('error'));

It seems to be working, but when i post (with errors) i log 'error' in the console, but i also get a POST project6.test/api/address 422 (Unprocessable Entity) error (in the console as well). Reading the laravel docs, it is expected that the controller will return a 422, so im wondering is the POST error to be expected (and therefore can be ignored) or is there something i should be doing to prevent it?

0 likes
2 replies
bugsysha's avatar

You just need to send all fields that you've specified in your validation rules to get passed 422. That is absolutely normal 422 when validation fails.

Please or to participate in this conversation.