gazd1977
1 month ago

422 (Unprocessable Entity) Error on ajax request

Posted 1 month ago by gazd1977

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?

Please sign in or create an account to participate in this conversation.