Sapnesh Naik
Sapnesh Naik

Reputation: 11636

Laravel - validation | The input field should be one of two values

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

Answers (2)

Ashiqur Rahman Emran
Ashiqur Rahman Emran

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

lagbox
lagbox

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

Related Questions