我的 Laravel 规则和正则表达式操作有一个小问题:
基本上,规则就是一个数组:
'room'=>'required|alpha_num|min:2|max:10',
我遇到的问题是使用正则表达式和 | 时(或)运算符,例如:
'cid'=>'required|regex:/^((comp)|(soen)|(engr)|(elec))\d{3}$/i',
我收到服务器错误:
ErrorException
preg_match(): No ending delimiter '/' found
我猜测 preg_match
停在 /.../
内的第一个 |
处。
有没有办法编写上面的代码来使其工作?
完整代码:
public static $rules = array(
'cid' => array('required', 'regex:/^((comp)|(soen)|(engr)|(elec))\d{3}$/i'),
'description'=>'required|regex:/^[A-Za-z \t]*$/i|min:3|unique:courses',
'credits'=>'required|regex:/^\d+(\.\d)?$/'
);
最佳答案
http://laravel.com/docs/validation#rule-regex
regex:pattern
The field under validation must match the given regular expression.
Note: When using the regex pattern, it may be necessary to specify rules in an array instead >of using pipe delimiters, especially if the regular expression contains a pipe character.
澄清一下: 你会做这样的事情
$rules = array('test' => array('size:5', 'regex:foo'));
关于php - Laravel 规则和正则表达式 (OR) 运算符的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60355745/