如何验证 Laravel 中的确切单词?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23509802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to validate exact words in Laravel?
提问by user742736
Does the validation class in Laravel validate exact words?
Laravel 中的验证类是否验证准确的单词?
eg
例如
$rules = [
"field" => "hello"
]
Where the value of the field must be "hello".
该字段的值必须是“hello”。
回答by Joel Hinz
Yes. I know of at least two ways.
是的。我知道至少有两种方法。
// option one: 'in' takes a comma-separated list of acceptable values
$rules = [
'field' => 'in:hello',
];
// option two: write a matching regular expression
$rules = [
'field' => 'regex:^hello$',
];
Actually, I don't remember if you need the ^$ delimiters in the regex, but it's easy to find out by trying it. :)
实际上,我不记得在正则表达式中是否需要 ^$ 分隔符,但是通过尝试很容易找到。:)
回答by Puneet
Method updated for Laravel 5.5. https://laravel.com/docs/5.5/validation#rule-in
Laravel 5.5 更新了方法。 https://laravel.com/docs/5.5/validation#rule-in
// 'in' takes an array of values
$rules = [
'field' => [ Rule::in('hello')]
];
Use Rule;
namespace in your controller to access Rule class (Illuminate\Validation\Rule)
Use Rule;
控制器中的命名空间以访问 Rule 类 (Illuminate\Validation\Rule)