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')
]);
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
You can add validation conditionally like this
$this->validate($request, [
'type' => 'required|in:opt,number,text,file,image',
'options'=>($input['type'] == 'opt')?'required':''
]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With