Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

php - Issue with Laravel Rules & Regex (OR) operator

I'm having a small issue with my Laravel rules and regex operation :

Basically a rule is an array as such :

'room'=>'required|alpha_num|min:2|max:10',

The problem i'm having is when using regex and the | (or) operator such as :

'cid'=>'required|regex:/^((comp)|(soen)|(engr)|(elec))d{3}$/i',

I'm getting a server error saying :

ErrorException

preg_match(): No ending delimiter '/' found

I'm guessing the preg_match is stopping at the first | inside the /.../.

Is there anyway to write the above code to make it work ?

Full code :

public static $rules = array(

'cid' => array('required', 'regex:/^((comp)|(soen)|(engr)|(elec))d{3}$/i'),

'description'=>'required|regex:/^[A-Za-z ]*$/i|min:3|unique:courses',

'credits'=>'required|regex:/^d+(.d)?$/'

);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

To clarify: You would do something like this

$rules = array('test' => array('size:5', 'regex:foo'));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...