Using the Pipe operator within Laravel validation rules
How to get past the validation error
If you get to a point where you need to add an 'or'(|) pipe operator within a regex validation rule, you'll notice that an error is thrown. I learnt that to overcome this, validation rules must be placed within an array. For example, lets say we have a validation rule which will check if a contact number starts with a certain number:
$request->validate([ 'email' => 'required|email|unique', 'contact_number' => 'regex:/0(7|1)[\d]+/' ]);
This will fail as it is trying to create a new validation rule just after the pipe. To solve this, the following is required:
$request->validate([ 'email' => 'required|email|unique', 'contact_number' => ['regex:/0(7|1)[\d]+/'] ]);
This lets the validator know that all validation rules will be specified within individual array entries.
Hope this helps.