Laravel如何在第一个错误后停止验证 [英] Laravel how to stop validation after first error
问题描述
我不知道如何使 laravel验证在发生第一个错误后停止验证,然后仅返回一个错误
I don't have a clue how to make laravel validate stop validation after first error occurs and then return only one error
带有val_前缀的规则是我的自定义规则.
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'
]);
我的验证码
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;
}
});
}
推荐答案
要在第一个规则失败的情况下停止验证,则应使用bail
,引用文档中的
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',
]);
在此示例中,如果title属性上的必需规则失败,则不会检查唯一规则.规则将按照分配的顺序进行验证.
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
这是github讨论 https://github.com/laravel/framework/issues/4789#issuecomment- 174837968
here is the github discussion https://github.com/laravel/framework/issues/4789#issuecomment-174837968
这篇关于Laravel如何在第一个错误后停止验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!