tayyab_fareed
tayyab_fareed

Reputation: 679

Laravel validate the field on the basis of value of another field

I want to validate two fields i.e. 'type' and 'options' where 'type' field is enum. The 'options' field should be validated only if the value of 'type' field is 'opt'.

$this->validate($request, [
    'type' => 'required|in:opt,number,text,file,image',
    'options'=>the condition I need(if type is 'opt')
]);

Upvotes: 6

Views: 8806

Answers (2)

SRK
SRK

Reputation: 3496

You can use required_if validation in Laravel.

$this->validate($request, [
    'type' => 'required|in:opt,number,text,file,image',
    'options'=> 'required_if:type,==,opt'
]);

Here is a Documentation link

Upvotes: 10

PPL
PPL

Reputation: 6555

You can add validation conditionally like this

$this->validate($request, [
        'type' => 'required|in:opt,number,text,file,image',
        'options'=>($input['type'] == 'opt')?'required':''
    ]);

Upvotes: 2

Related Questions