Laravel请求验证正则表达式,无需 [英] Laravel Request Validation Regex without Required
问题描述
我对请求验证有疑问.一切正常,但只有一种模式.如果我不希望该字段为必填字段,但一旦填写完毕,我希望它匹配一个正则表达式.
I have a problem with Request validation. Everything works but one pattern. And its if I dont want the field to be required but once it is filled I want it to match a regex.
但是当我将该字段留空时,它将引发正则表达式错误.
But it throws regex error when I leave the field empty.
有关如何处理的任何提示? 顺便说一句:我做了一个自定义的Request类,负责验证工作,因此,如果解决方案也可以出现在Request中,而不是直接出现在Controller中,那就太好了.
Any tips on how I should handle it? BTW: I made a custom Request class where I take care of the validation so if the solution could be also in the Request and not directly in Controller that would be great.
return [
'dic' => 'max:12|regex:/^[a-zA-Z]{2}[0-9]{8}[0-9]*$/',
];
推荐答案
return [
'dic' => 'nullable|max:12|regex:/^[a-zA-Z]{2}[0-9]{8}[0-9]*$/',
];
nullable
在该字段为空时不会检查其他规则.
nullable
won't check the other rules when the field is empty.
这篇关于Laravel请求验证正则表达式,无需的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!