Issue with Laravel Rules & Regex (OR) operator
I use this style and save my life :-)
change code from
$validator = Validator::make(
$request->all(),
[
'name' => 'required|string',
'initial_credit' => 'required|integer|between:0,1000000|regex:/[1-9][0-9]*0000$/'
]
]);
to
$validator = Validator::make(
$request->all(),
[
'name' => 'required|string',
'initial_credit' => [ // <=== Convert To Array
'required',
'integer',
'between:0,1000000',
'regex:/([1-9][0-9]*0000$)|([0])/' // <=== Use pipe | in regex
] // <=== End Array
]);
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'));
You should use an array
instead of separating rules using |
:
'cid' => array('required', 'regex:/^((comp)|(soen)|(engr)|(elec))\d{3}$/i')
The pipe (|
) sigh is available in your regular expression pattern so it's conflicting with the separator. Other answer already stated it.