Laravel模式验证管道字符问题 [英] Laravel pattern validation pipe character issue

查看:45
本文介绍了Laravel模式验证管道字符问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Laravel框架,我需要对照一组有效值来检查提交字段的值.

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.

这不起作用,它抛出了

This does not work, it throws a

"preg_match:未找到定界符'/'":

"preg_match: No ending delimiter '/' found":

'my_field' => 'match:/^(value1|value2|different value|yet another)$/'

如果\字符无用,在这种情况下如何逃避|字符?

How does one escapes the | character in this case if the \ character does not help?

推荐答案

从本质上讲,答案是,如果要像尝试执行的那样在一个字符串中指定所有规则,则不能使用管道. buettner提到的拉取请求已关闭.但是,Tayler Otwell提到了可以使用的另一种方法:在数组中指定规则.例如:

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:

$rules = array(
    'field' => array('size:5', 'match:/foo|bar/')
);

这篇关于Laravel模式验证管道字符问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆