Validating Matching Strings

限于喜欢 提交于 2019-12-07 00:20:05

问题


When I use the Validation feature in Laravel, how can I add a pre-defined strings that are allowed in an Input?

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
]);

回答1:


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)"]  
]);



回答2:


Best to use the 'in' validation rule like this:

$validator = Validator::make($credentials, [
      'profile' => ["required" , "max:255", "in:foo,bar,baz"]  
]);


来源:https://stackoverflow.com/questions/31439203/validating-matching-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!