Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel how to stop validation after first error

I don't have a clue how to make laravel validate stop validation after first error occurs and then return only one error

Rules with val_ prefix are my custom rules.

I need to display only error for pesel field if the pesel field is empty('required' rule) Could anyone tell me how can I reach that ?

$this->validate($request, [

    'pesel'=> 'bail|required|val_pesel',
    'dane_os' => 'required',
    'id_project' => 'required',
    'imie' => 'required|val_imie',
    'nazwisko'=>'required|val_nazwisko',
    'nazwisko_matki'=>'required|val_nazwisko_matki'

]);

MY VALIDATION CODE

Validator::extend('val_pesel',function($attribute,$value,$parameters,$validator)
        {

      $val = DB::select('select * from  `wyborca` where pesel = "'.$value.'"  ; ');
      if(empty($val))
      {
        return false;
      }
      else {
        return true;
      }
    });
    Validator::extend('val_imie',function($attribute,$value,$parameters,$validator)
    {
       $test = $validator->getData();
       $pesel = $test['pesel'];
       $imie = $test['imie'];




      $val = DB::select('select * from  `wyborca` where pesel = "'.$pesel.'" and imie = "'.$imie.'" ; ');
      if(empty($val))
      {
        return false;
      }
      else {
        return true;
      }
    });
    Validator::extend('val_nazwisko',function($attribute,$value,$parameters,$validator)
    {
       $test = $validator->getData();
       $pesel = $test['pesel'];
       $nazwisko = $test['nazwisko'];




      $val = DB::select('select * from  `wyborca` where pesel = "'.$pesel.'" and nazwisko = "'.$nazwisko.'" ; ');
      if(empty($val))
      {
        return false;
      }
      else {
        return true;
      }
    });
    Validator::extend('val_nazwisko_matki',function($attribute,$value,$parameters,$validator)
    {
       $test = $validator->getData();
       $pesel = $test['pesel'];
       $nazwisko_matki = $test['nazwisko_matki'];




      $val = DB::select('select * from  `wyborca` where pesel = "'.$pesel.'" and nazwisko_matki = "'.$nazwisko_matki.'" ; ');
      if(empty($val))
      {
        return false;
      }
      else {
        return true;
      }
    });
    Validator::extend('vote_exists',function($attribute,$value,$parameters,$validator)
    {
       $test = $validator->getData();
       $pesel = $test['pesel'];




      $val = DB::select('select * from  `glosy` where pesel = "'.$pesel.'"  ; ');
      if(empty($val))
      {
        return false;
      }
      else {
        return true;
      }
    });

}
like image 808
Wini the Pooh Avatar asked Sep 02 '17 11:09

Wini the Pooh


3 Answers

To stop validation if the first rule fails you should use bail, quote from docs

Stopping On First Validation Failure Sometimes you may wish to stop running validation rules on an attribute after the first validation failure. To do so, assign the bail rule to the attribute:

$this->validate($request, [
    'title' => 'bail|required|unique:posts|max:255',
    'body' => 'required',
]);

In this example, if the required rule on the title attribute fails, the unique rule will not be checked. Rules will be validated in the order they are assigned.

https://laravel.com/docs/5.5/validation

here is the github discussion https://github.com/laravel/framework/issues/4789#issuecomment-174837968

like image 177
dav Avatar answered Nov 20 '22 19:11

dav


I know this question is old, but I had the same problem today, so I'll document what I found out:

In Laravel 8.30.0 or newer, there are 2 ways to do it:

1. Using a FormRequest

Set the following attribute in your FormRequest class:

protected $stopOnFirstFailure = true;

2. Using a Validator

Manually create a Validator, and call the stopOnFirstFailure(true) method on it, as in:

use Illuminate\Support\Facades\Validator;

$validator = Validator::make($request->all(), [
   // your validation rules
])->stopOnFirstFailure(true);

$validator->validate();
like image 27
Amade Avatar answered Nov 20 '22 20:11

Amade


To redirect and stop validation after the first rule has failed you can use the 'bail' keyword. an example:

    // $arr = ['form input var'=>'exact word to be used in error msg'] ***

    $arr = ['coverletter'=>'Cover Letter','vitae'=>'CV','certificate'=>'Certificate','tesol'=>'Tesol','photo'=>'Photo'];

    // $this->validate($data,$rules,$messages);
    // we have 2 rules to validate, 'required' & 'mimetypes' so output messages for them must be in an array

    foreach ($arr as $k=>$v) {
        if($k !== 'photo') {

    // Tesol, CV, Certificate must be in PDF format so we can loop through all 3 of them here

        if(!Session::get($k) || $request->$k) {     

    // user has submitted before but is submitting again, we evaluate the request
    // user has not submitted before, we evaluate the request   

            $this->validate($request,
                [$k => 'bail|required|mimetypes:application/pdf'],
                [$k . '.required' => $v . ' is required',
                $k . '.mimetypes' => $v . ' must be PDF']);
            }
        }
        if($k == 'photo') {

            if(!Session::get($k) || $request->$k) {         

            $this->validate($request,
                [$k => 'bail|required|mimetypes:image/jpg,image/jpeg'],
                [$k . '.required' => $v . ' is required',
                $k . '.mimetypes' => $v . ' must be of type jpeg']);
            }
        }
    }

this will redirect back with your error message after the first rule failed.

like image 1
Bruce Tong Avatar answered Nov 20 '22 21:11

Bruce Tong