问题描述
在我的Laravel 5.2应用程序中,我具有以下验证规则来检查字段:
In my Laravel 5.2 app, I have the following validation rules to check a field:
$rules = array(
'file' => 'mimes:jpg,png,pdf,doc,docx',
'file' => 'validate_file'
);
现在,validate_file
是自定义验证规则.我只想仅在第一个mimes
验证通过的情况下运行它.
Now, the validate_file
is a custom validation rule. I want to run it only only if the first mimes
validation passes.
我知道我可以简单地通过错误进行验证/重定向-分两批进行,首先是mimes
,然后是validate_file
,但这似乎有点过头了.
I know I could simply validate/redirect with errors - in two batches, first the mimes
, then the validate_file
, but it seems like an overkill.
有什么方法可以在Laravel 5.2中做到这一点吗?
Any way I can do it the smart way in Laravel 5.2?
推荐答案
使用保释"属性.第一次失败后它将停止验证.
Use "bail" attribute. It will stop validation after first failure.
$this->validate($request, [
'title' => 'bail|mimes:jpg,png,pdf,doc,docx|validate_file',
]);
https://laravel.com/docs/5.2/validation#validation-quickstart 搜索保释金"
这篇关于Laravel:仅当另一个通过验证规则时才应用验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!