Laravel 模式验证管道字符问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12875334/
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
Laravel pattern validation pipe character issue
提问by Wabbitseason
Using the Laravel framework I need to check the value of a submitted field against a set of valid values.
使用 Laravel 框架,我需要根据一组有效值检查提交字段的值。
The problem is Laravel uses the | character to separate validation rules belonging to a field.
问题是 Laravel 使用了 | 字符来分隔属于一个字段的验证规则。
This does notwork, it throws a
这并不能正常工作,它抛出一个
"preg_match: No ending delimiter '/' found":
“preg_match:未找到结束分隔符‘/’”:
'my_field' => 'match:/^(value1|value2|different value|yet another)$/'
How does one escapes the |
character in this case if the \
character does not help?
|
如果\
字符没有帮助,在这种情况下如何转义字符?
回答by BenjaminRH
The answer, essentially, is that you cannot use a pipe if you're specifying all the rules in one string like you're trying to do. The pull request that m.buettner mentioned was closed. However, Tayler Otwell mentioned an alternative method you can use: specify the rules in an array. An example of this would be:
答案基本上是,如果您像尝试那样在一个字符串中指定所有规则,则不能使用管道。m.buettner 提到的拉取请求已关闭。但是,Tayler Otwell 提到了一种您可以使用的替代方法:在数组中指定规则。这方面的一个例子是:
$rules = array(
'field' => array('size:5', 'match:/foo|bar/')
);
回答by faisal khan
After using array() ,regular expression pattern needs to be enclosed in /.../ to avoid preg_match(): Unknown modifier '|'error
使用 array() 后,正则表达式模式需要包含在 /.../ 中以避免preg_match(): Unknown modifier '|' 错误