title: How to validate X-editable request in laravel (Server side)
body: <p>I've managed to get x-editable (<a href="https://vitalets.github.io/x-editable/" rel="nofollow noreferrer">https://vitalets.github.io/x-editable/</a>) to work with a page, in so far as when I click on a field, it displays inline and I can edit it, and I'm able to successfully submit to a POST URI.</p>
  
  <p>The idea here is that I'm sending three key-value pairs:</p>
  
  <pre><code>array:3 [▼
    "name" =&gt; "name"
    "value" =&gt; "mathematics"
    "pk" =&gt; "1"
  ]
  </code></pre>
  
  <p>and my <code>update()</code> method catches the array, and it successfully updates the record in the database. But I'm failing to validate the data. </p>
  
  <p>This is how my controller looks:</p>
  
  <pre><code> public function update(Request $request)
  {
      //
      $id = $request-&gt;pk;
      $subject = Subject::findOrFail($id);
  
      $rules = array (
          'name' =&gt; 'bail|required|max:20|unique:subjects,name,'.$id
      );
  </code></pre>
  
  <p><strong>This validation pass easily even if I try to fail it</strong></p>
  
  <pre><code>    $validator = Validator::make ( $request-&gt;all(), $rules );
  
      if ($validator-&gt;fails ()) {
          return response()-&gt;json ( array (
  
           'errors' =&gt; $validator-&gt;getMessageBag ()-&gt;toArray ()
          ) );
      } else {
          $subject-&gt;update([$request-&gt;name =&gt; $request-&gt;value]);
      }
      return response ()-&gt;json ( $subject );
   }
  </code></pre>
  
  <p>So it's as if I'm somehow not passing the "correct" Request object to validate()? There is no form submission, but <a href="https://laravel.com/docs/5.4/validation" rel="nofollow noreferrer">the documentation</a> clearly states that: </p>
  
  <blockquote>
    <p>Laravel generates a JSON response containing all of the validation errors. This JSON response will be sent with a 422 HTTP status code.<sup><a href="https://laravel.com/docs/5.4/validation#quick-ajax-requests-and-validation" rel="nofollow noreferrer">1</a></sup></p>
  </blockquote>
  
  <p>Route:</p>
  
  <pre><code>Route::post('/subjects/update/', 'SubjectsController@update');
  </code></pre>
  
  <p>script:</p>
  
  <pre><code>$('#subjects').editable({
       container:'body',
       selector:'td.name',
       type:'post',
       dataType:'JSON',
       validate:function(value){
          if ($.trim(value) === '') {
             return "Field is required";
          }
       }
  });
  </code></pre>
  
  <hr>
  
  <p><sup>1</sup><sub><a href="https://laravel.com/docs/5.4/validation#quick-ajax-requests-and-validation" rel="nofollow noreferrer">https://laravel.com/docs/5.4/validation#quick-ajax-requests-and-validation</a></sub></p>