title: Looping through validation data received from Ajax in laravel controller
body: <p>I am sending a json request through Ajax, this request contains multiple objects,I am trying to find a way to validate the request to fail if at least one particular attribute fails the rule(min 5 characters in &quot;value&quot;); but dosent seems to work, can anyone help ?
  I tried to loop data , I feel I am close but still dosent work.</p>
  <pre><code>my Ajax:
  -------    
  &lt; script &gt;
    function ajaxUpdate() {
      var formdata = document.getElementsByTagName('input'); 
      var formT = [].map.call(formdata, function(input) {
  
        if (input.style.textDecoration === &quot;line-through&quot;) {
          input['completed'] = &quot;1&quot;; //add key value pairs
        } else {
          input['completed'] = &quot;0&quot;; //add key value pairs
        }
        return {
          'value': input.value,
          'id': input.id,
          'completed': input.completed
        }; //determine what keys to show.
      });
  
      formT.shift(); // Removes the first element from an array, because it contains token
      formT = formT.filter(function(e) {
        return e != null; //remove null elements.
      });
  
      $.ajaxSetup({
        headers: {
          'X-CSRF-TOKEN': $('meta[name=&quot;csrf-token&quot;]').attr('content')
        }
      });
      // e.preventDefault();
  
  
      $.ajax({
        url: &quot;/updateAddTask&quot;,
        dataType: &quot;json&quot;,
        contentType: &quot;application/json&quot;,
        data: {
          objJSON: JSON.stringify(formT),
        },
  
        success: function(response) {
  
          if ($.isEmptyObject(response.error)) {
  
            if (response.success === true) {
              successSweetAlert();
            } else {
              alert(JSON.stringify(response.errors.value.toString()));
            }
  
          }
        },
        // location.reload();
      })
    } 
  &lt;/script&gt;
  
  
  
  My controller:
  --------------
  
    public function loopData(Array $data)
  {
      foreach ($data  as $key =&gt; $jsons) {
          foreach ($jsons as $key1 =&gt; $value) {
              return $jsons;
          }
      }
  }
     
    public function ajaxUpdateTasks(Request $request)
  
      {
          $data = json_decode($request-&gt;objJSON, true);//this will give an array of all the input elements
  
  // dd($data);//output is below:
  // array:3 [
  //  0 =&gt; array:3 [
  //    &quot;value&quot; =&gt; &quot;kilo&quot;
  //    &quot;id&quot; =&gt; &quot;55&quot;
  //    &quot;completed&quot; =&gt; &quot;0&quot;
  //  ]
  //  1 =&gt; array:3 [
  //    &quot;value&quot; =&gt; &quot;new123&quot;
  //    &quot;id&quot; =&gt; &quot;793&quot;
  //    &quot;completed&quot; =&gt; &quot;1&quot;
  //  ]
  //  2 =&gt; array:3 [
  //    &quot;value&quot; =&gt; &quot;ef4&quot;
  //    &quot;id&quot; =&gt; &quot;794&quot;
  //    &quot;completed&quot; =&gt; &quot;0&quot;
  //  ]
  //]
  
  
          $rules = [
              'value' =&gt; 'required|max:255|min:5', //value is
          ];
  
  
          $messages = [
              'value.max' =&gt; 'Todo title should be less than 255 characters',
              'value.min' =&gt; 'Todo title should be more than 4 characters'
          ];
  
  
          $validator = Validator::make($this-&gt;loopData($data),$rules, $messages);// ?????
  
          if ($validator-&gt;fails()) {
  
              $errors = $validator-&gt;getMessageBag()-&gt;toArray();
              $result = ['success' =&gt; false, 'errors' =&gt; $errors];
              return response()-&gt;json($result);
          } else {
              $result = ['success' =&gt; true, 'errors' =&gt; null];
  
              $count = count(json_decode($request-&gt;objJSON, true));
              $id = json_decode($request-&gt;objJSON, true)[0] ['id'];
              $value = json_decode($request-&gt;objJSON, true)[0] ['value'];
              $completed = json_decode($request-&gt;objJSON, true)[0] ['completed'];
              
              
              -----remaining of the code --------
      }
      
  </code></pre>
  <p>Thanks for any help.</p>