Laravel Error Message : 422 Unprocessable Entity

Posted on

This is an article which is used to discuss about an error message generated by a web-based application which is powered on Laravel framework. That error message is “422 Unprocessable Entity.”. That error itself is mentioned in the Laravel Documention specifically in the Validation part. Below is the explanation given in Laravel Documentation page for the validation using an AJAX request which is sending a form’s input message :

AJAX Requests & Validation

In this example, we used a traditional form to send data to the application. However, many applications use AJAX requests. When using the validate method during an AJAX request, Laravel will not generate a redirect response. Instead, Laravel generates a JSON response containing all of the validation errors. This JSON response will be sent with a 422 HTTP status code.

The scenario which is triggered an error specified in the title of this article, “422 Unprocessable Entity” is acquired when a form input message is sent using an AJAX request. This request finally will be processed through a specific controller file which is already defined in the router file definition of a Laravel web-based application.

For an example if we have a page consisting of a form as shown below which is defined in a blade view file template :

{{ Form::open(array('url' => '/server/check_general', 'class' => 'form-horizontal', 'id' => 'home_form'))}}
...
...
...
<input type="text" class="form-control" placeholder="IP Address" name="ip_address" />
...
{{ Form::close }}

The above snippet code is focusing on defining a form with an input type field which is solely to acquire an input from the user which is the IP Address of the Server information. As it can be seen in the above snippet code, it is a CRUD (Create Update Delete) web-based application in the Create form part.

It is a form for creating a new Server record by inserting certain data or information including IP Address as shown above.

As it can be seen in the snippet code, the form will be submitted to the URL named ‘/server/check_general’. It is defined in a router file named ‘web.php’ located in a folder named routes. Below is the declaration of the route for the URL mentioned previously :

Route::post('/server/check_general','ServerController@checkGeneral');

Submitting the form will occur on retrieving several information from it but since it is using AJAX request to post the form, there will be a error generated as 422 Unprocessable entity shown. Below is the AJAX request part for sending the form :

            
$("#home_form").submit(function (e) {
   init();
          $.ajax({
                    url: "{{url('/server/check_general')}}",
                    type: "POST",
                    data: $("#home_form").serialize(),
                    success: function (data) {
                        ...
                    }
          });
          e.preventDefault();
});

And the validation snippet code defined is shown in the method which is ‘checkGeneral’. And it is written in a Controller file named ‘ServerController’ :

public function checkGeneral(Request $request) {
$validator = $this->validate($request, 
      ['ip_address' => 'ip',],
      ['ip_address.ip' => 'IP Address must be in IP Address format']
      );
...
}

The error will not be printed in the blade view template although it is being returned. It is returned with the error message ‘422 Unprocessable Entity’. To be able to process it by retrieving it into a single variable which later on can be written, it can be done by adding the following line in the AJAX Request script :

$("#home_form").submit(function (e) {
   init();
          $.ajax({
               url: "{{url('/server/check_general')}}",
               type: "POST",
               data: $("#home_form").serialize(),
               success: function (data) {
                    ...
               },
               error: function (data)        
               {                                   
                    $("#ip_address").text(data.responseJSON.ip_public); 
               }
          });
}

So, the error will be printed also in the blade view template. Just add a label element which is going to display the error message initiated in the AJAX Request script as shown above.

One thought on “Laravel Error Message : 422 Unprocessable Entity

Leave a Reply