title: Laravel Ajax validation
body: <p>I understand how to validate requests by type-hinting the class name in the controller method. However for Ajax requests, <a href="http://laravel.com/docs/5.0/validation#controller-validation" rel="nofollow">According to the documentation</a>, I should validate data in the controller, because using a validator class will redirect rather than send a response.</p>
  
  <p>The main part I'm looking at is this:</p>
  
  <blockquote>
    <p>If the incoming request was an AJAX request, no redirect will be
    generated.  Instead, an HTTP response with a 422 status code will be
    returned to the browser containing a JSON representation of the validation errors.</p>
  </blockquote>
  
  <p>However, my controller is as follows:</p>
  
  <pre><code>public function update(App\Permission $permission, Request $request)
  {
      $this-&gt;validate($request, [
          'permission_description' =&gt; 'required|string'
      ]);
  
      ...
  }
  </code></pre>
  
  <p>And I can't for the life of me get it to respond with JSON. The documentation states that if it fails, it throws an <code>Illuminate\Contracts\Validation\ValidationException</code> exception, but I can't catch it. </p>
  
  <p>Whenever it fails, it always redirects back to the edit page. Obviously I don't want this, I want the json response.</p>
  
  <p>I have just tried "manually writing it out" with the whole <code>$v = Validator::make($request-&gt;all(), ...);</code> which does work, but what's the point in using the <code>$this-&gt;validate()</code> way if it doesn't work?</p>
  
  <p>Does the <code>$this-&gt;validate()</code> method just not work with AJAX and I have to write it the long way each time? Am I doing something wrong?! </p>
  
  <p>Below is what I've tried:</p>
  
  <pre><code>public function update(App\Permission $permission, UpdatePermissionRequest $request)
  {
     /** Redirects rather than returns JSON if the validation fails **/
  }
  
  ----------------------------------
  
  public function update(App\Permission $permission, Request $request)
  {
      $this-&gt;validate($request, [
          'permission_description' =&gt; 'required|string'
      ]);
  
      /** AND I've also tried: **/
  
      try {
          $this-&gt;validate($request, ['permission_description' =&gt; 'required|string']);
      } catch (\Illuminate\Contracts\Validation\ValidationException $e {
          echo $e; /** Echoing for debug reasons **/
          exit;
      }
  
      ... 
      /** Still redirects the browser, even if it is an AJAX request **/
  }
  
  -----------------------------------------
  
  use Validator;
  ...
  public function update(App\Permission $permission, Request $request)
  {
      $v = Validator::make($request-&gt;all(), [
          'permission_description' =&gt; 'required|string'
      ]);
  
      if($v-&gt;fails())
      {
          return response()-&gt;json(['reply' =&gt; false]);
      }
  
      /** Works **/
  }
  </code></pre>
  
  <p><strong>UPDATE</strong>
  The documentation is incorrect. It states that the <code>$this-&gt;validate()</code> method throws a <code>Illuminate\Contracts\Validation\ValidationException</code> but it doesn't. It throws a <code>Illuminate\Http\Exception\HttpResponseException</code> exception.</p>