我不知道在发生第一次错误后如何使 laravel validate 停止验证然后只返回一个错误
带有val_前缀的规则是我的自定义规则。
如果 pesel 字段为空(“必需”规则),我只需显示 pesel 字段的错误 谁能告诉我怎样才能达到这个目标?
$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'
]);
我的验证码
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;
}
});
}
答案 0 :(得分:2)
如果第一条规则失败,要停止验证,请使用bail
,引用文档
停止首次验证失败 有时您可能希望在第一次验证失败后停止对属性运行验证规则。为此,请将保释规则分配给属性:
$this->validate($request, [
'title' => 'bail|required|unique:posts|max:255',
'body' => 'required',
]);
在此示例中,如果title属性上的必需规则失败,则不会检查唯一规则。规则将按照分配的顺序进行验证。
https://laravel.com/docs/5.5/validation
这是github的讨论 https://github.com/laravel/framework/issues/4789#issuecomment-174837968
答案 1 :(得分:1)
要在第一条规则失败后重定向并停止验证,您可以使用'保释'关键词。一个例子:
// $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']);
}
}
}
在第一个规则失败后,这将重定向并显示错误消息。
答案 2 :(得分:1)
我知道这个问题很老,但我今天遇到了同样的问题,所以我会记录我的发现:
In Laravel 8.30.0
或更新版本,有两种方法:
在您的 FormRequest 类中设置以下属性:
protected $stopOnFirstFailure = true;
Manually create a Validator,并对其调用 stopOnFirstFailure(true)
方法,如:
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
// your validation rules
])->stopOnFirstFailure(true);
$validator->validate();