Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
716 views
in Technique[技术] by (71.8m points)

php - 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;
      }
    });

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...