1

I have a validator, it has error messages, and my goal is to get only error messages with the field names.

$validator = Validator::make(
   array(
      'firstField' => Input::get('firstFieldName');
      'secondField' => Input::get('secondFieldName');
   ),
   array(
      'firstField' => 'required';
      'secondField' => 'required';
   )
);

if($validator->fails()){
   return $validator->messages();
}

So, this piece of code is returning some values to my js file as following

function getErrorMessages(){
   //Some UI functions
   var myDiv = document.getElementById('messageDivID');
   $.post('validationRoute', function(returedValidatorMessageData)){
      for(var a in returedValidatorMessageData){
         myDiv.innerHTML = myDiv.value + a;
      }
   }
}

Problem is, the only values i get from post request is like firstField secondField but i'd like to get like firstField is required message. What would be the easiest way ?

Thanks in advance.

3 Answers 3

1

Laravel's messages() method returns array with this format:

[
    "first_name" => [
        "First name is required."
    ]
]

Where keys are name of the field and values are arrays of error messages. So just modify your js using values instead of keys. Example:

for (var key in returedValidatorMessageData){
     console.log(returedValidatorMessageData[key]);
}
Sign up to request clarification or add additional context in comments.

Comments

1

It's not a perfect approach i know, but created my own way as the answer :

if($validator->fails()){
       //get all the messages
       $errorArray = $validator->errors()->all(':message');
          //& operator means changes will affect $errorArray
          foreach($errorArray as &$a){
             $a = ucfirst($a);           //uppercases first letter (you can do this in laravel config too)
             $a = '<li>'.$a.'</li>';     //add to list
           }
       //add a string at the beginning of error message
       array_unshift($errorArray, '<p><b>The error messages are following :</b></p>');
       //implode and return the value
       return implode(' ',$errorArray);
}

Comments

0

You should search in laravel official documentation... in the view use this

<!-- /resources/views/post/create.blade.php -->

<h1>Create Post</h1>

@if (count($errors) > 0)
<div class="alert alert-danger">
    <ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
</div>
@endif

<!-- Create Post Form -->


 in controller :

public function name(Request $request)
{
    $validator = Validator::make($request->all(), [
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ]);

    if ($validator->fails()) {
        return redirect('post/create')
                    ->withErrors($validator)
                    ->withInput();
    }

    // Store the blog post...
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.