19

If there a way to check whether or not the validator failed specifically because of the unique rule?

$rules = array(
            'email_address' => 'required|email|unique:users,email',
            'postal_code' => 'required|alpha_num',
        );

        $messages = array(
            'required' => 'The :attribute field is required',
            'email' => 'The :attribute field is required',
            'alpha_num' => 'The :attribute field must only be letters and numbers (no spaces)'
        );

        $validator = Validator::make(Input::all(), $rules, $messages);

        if ($validator->fails()) {

In laymans terms, I basically want to know: "did the validation fail because the email_address was not unique?"

2
  • Maybe provide an input which passes the other validation rules? (And checking the error messages). Commented Aug 29, 2014 at 17:51
  • But you can see exactly what inputs I'm checking for ... email_address and postal_code Commented Aug 29, 2014 at 18:19

2 Answers 2

37

Check for a specific rule within the returned array of failed rules

if ($validator->fails()) {

    $failedRules = $validator->failed();

    if(isset($failedRules['email_address']['Unique'])) {

    ...
Sign up to request clarification or add additional context in comments.

4 Comments

Converted to json, it seems to return {"email_address":{"Unique":["users","email"]}}
updated. You have to check against it by field, each field having its own array of rules.
Almost, but it's still always returning false, even though I can see that it's true.
Updated, the rule Unique is a key, so in_array didn't work :) so instead I used isset
1

This will display an error and tell you what failed:

Controller

if($validation->fails()){

   return Redirect::back()->withErrors($validation)->withInput();
}

foreach($errors->all() as $error) {
  echo $error;
}

And in your blade template add this:

   @foreach($errors->all() as $error)
        <div>
           {{$error}}
        </div>
   @endforeach

And that will return a message with whatever the error is. Email doesn't match. Field is required. Blah blah

You can also remove that email array from the $message. The validator will handle all that for you. You only want to use that if you want custom messages.

You can also try to var_dump this statement:

var_dump($validation->errors()); die;

5 Comments

Yes, but I need to know what failed within my controller, before getting to the view.
Well in your controller you can use that same foreach and it will display in your controller. I edited my answer so you can see
$errors->all() produces Call to a member function all() on a non-object
Sorry, yeah that won't work because it's supposed to redirect back to your previous page anyways. I will update the answer.
Check my answer you may just be able to var dump the results using var_dump($validation->errors()); die;

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.