Laravel 如何在第一次错误后停止验证

Win*_*ooh 7 php validation laravel

我不知道如何在第一个错误发生后使laravel 验证停止验证,然后只返回一个错误

带有 val_ 前缀的规则是我的自定义规则。

如果pesel字段为空,我只需要显示pesel字段的错误('required' 规则) 谁能告诉我我怎样才能达到那个?

$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'

]);
Run Code Online (Sandbox Code Playgroud)

我的验证码

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

}
Run Code Online (Sandbox Code Playgroud)

Ama*_*ade 17

我知道这个问题很老了,但我今天遇到了同样的问题,所以我将记录我发现的内容:

在 Laravel8.30.0或更新版本中,有两种方法可以做到这一点:

1. 使用表单请求

在FormRequest类中设置以下属性:

protected $stopOnFirstFailure = true;
Run Code Online (Sandbox Code Playgroud)

2. 使用验证器

手动创建一个 Validator,并调用stopOnFirstFailure(true)它的方法,如下所示:

use Illuminate\Support\Facades\Validator;

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

$validator->validate();
Run Code Online (Sandbox Code Playgroud)


dav*_*dav 14

要在第一条规则失败时停止验证,您应该使用bail,引用文档

在第一次验证失败时停止 有时您可能希望在第一次验证失败后停止对属性运行验证规则。为此,请将保释规则分配给属性:

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

在本例中,如果title 属性上的required 规则失败,则不会检查唯一规则。规则将按照分配的顺序进行验证。

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

这是 github 讨论 https://github.com/laravel/framework/issues/4789#issuecomment-174837968

  • 不正确的答案。'pesel' 规则已经有 'bail' ......它会停止但其他规则“dane_os”、“id_project”将继续验证......如果“pessel”失败则需要其他规则,如“dane_os”、“id_project”不被已验证......“保释”仅对当前字段停止验证(在“pesel”情况下),但其他字段将继续验证 (9认同)

Eri*_*cía 0

您可以只使用bail验证规则:

    // Get withdraw requests here
    $data = $request->validate([
        'pay_method' => 'bail|required',
        'amount' => ['required', 'gte:' . (Coin::where('tick', $request->input('pay_method'))->get())->min_out, 'lte:' . $user->balance],
        'receiving' => 'required|gt:0|lt:10000',
        'required_data' => 'required',
        'user' => 'required',
        'uuid' => 'required|uuid',
        'data' => 'required'
    ]);
Run Code Online (Sandbox Code Playgroud)