Laravel Validation: check why validator failed

后端 未结 2 1984
长发绾君心
长发绾君心 2021-02-02 00:36

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

$rules = array(
            \'email_address\' =         


        
相关标签:
2条回答
  • 2021-02-02 00:48

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

    if ($validator->fails()) {
    
        $failedRules = $validator->failed();
    
        if(isset($failedRules['email_address']['Unique'])) {
    
        ...
    
    0 讨论(0)
  • 2021-02-02 00:54

    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;

    0 讨论(0)
提交回复
热议问题