在Laravel中使用验证功能时,如何添加输入中允许的预定义字符串?
When I use the Validation feature in Laravel, how can I add a pre-defined strings that are allowed in an Input?
例如,假设我希望输入仅包含以下之一:foo,bar,baz,我该怎么做?
For example, let's say I want the Input to contain only one of the following: foo,bar,baz, how can I do that?
$validator = Validator::make($credentials, [ 'profile' => 'required|max:255', // Here I want Predefined allowed values for it ]);推荐答案
在Laravel文档中,建议当验证规则变得更大并且我认为3条规则就足够时,将验证规则放入数组中.我认为它使内容更具可读性,但您不必这样做.我在下面添加了regex部分,并且它可以正常工作.我对重排东西不是很好.让我知道.
It is recommended in Laravel docs to put the validation rules in an array when they get bigger and I thought 3 rules were sufficient. I think it makes it for a more readable content but you do not have to. I added the regex portion below and I have it works. I am not that great with regexing stuff. Let me know.
$validator = Validator::make($credentials, [ 'profile' => ["required" , "max:255", "regex:(foo|bar|baz)"] ]);更多推荐
验证匹配字符串
发布评论