Reputation: 11636
I am trying to validate a form request and I want to accept the field say test
if only it has a value or ABC
or XYZ
. How can I achieve this?
I currently have
$request->validate([
'test' => 'required|unique:tests',
]);
Upvotes: 3
Views: 12617
Reputation: 207
You can use regular expression to validate this field
your regular expression for ABC OR XYZ
Code is
$request->validate([
'test' => 'required|unique:tests|regex:/ABC|XYZ/g',
]);
Upvotes: 2
Reputation: 50491
The Laravel docs are quite helpful when it comes to these issues.
Laravel Docs - Validation - Available Methods
Laravel Docs - Validation - Rule - in
'test' => [
'required',
Rule::in(['ABC', 'XYZ']),
]
or
'test' => 'required|in:ABC,XYZ',
Upvotes: 13