0

I need to add multiple custom error messages like this and both needs to be displayed as validations messages. I have the following code :

 if (!$condition1) {
     $error[] = "Condition 1 needs to be satisfied";
     $validation = false;
 }

 if (!$condition2) {
     $error[] = "Condition 2 needs to be satisfied";    
     $validation = false;
 }

 $validator->setCustomMessages($error);

But here I am getting only one message that is the first one even if it is entering to second condition. I have tried to add $validator->setCustomMessages("Message"); in each of the conditions, but it is also doing the same thing.

3
  • When do you see that you have only one error? after $validator->setCustomMessages($error); or in the view? Commented Apr 4, 2017 at 14:03
  • As an API call response. Commented Apr 4, 2017 at 14:03
  • Are you extending the Validation class? Setting the custom messages you should add the name of the rule as index of your $error array Commented Apr 4, 2017 at 14:08

2 Answers 2

1

Why are you using conditionals for validation? This is what I use to handle validation for uploading photos in my controller. You have a custom error message for 'required' and one for 'size' (which refers to the size of the photos array). To add another custom message, just pipe on another rule and its corresponding error message using dot notation. Only after all rules are satisfied then the code continues execution.

$this->validate($request,
    [
        'photos'        => 'required|size:4'
    ],
    [
        'photos.required'       => 'Photos are Required',
        'photos.size'           => 'You must upload 4 Photos'
    ]
);
Sign up to request clarification or add additional context in comments.

Comments

0

Try to change $validator->setCustomMessages($error); for $validator->getMessageBag()->merge($error);

Now if you do $validator->errors()->getMessages() you must have your array with both errors

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.