Skip to content
Advertisement

How to create 2 required validations Laravel?

I have a couple of form fields. The first is a yes/no question. If the answer is no, you must fill out one of four other fields. If the answer is yes, you don’t have to fill any of them. I try to use require_without to check if any one of these fields is filled out.

The problem is the validation. If I use

'field1' => 'required_if:Question,N' 

in the validation, it will require all four fields. I only want to require any one. But if I use

'field1' =>'require_without:field2,field3,field4,'

then it requires the fields whether the answer to the question is yes or no!

Is there any way to combine both, so that it only requires the fields if BOTH statements are true, rather than either? Combining both makes it only require one of the conditions.

Advertisement

Answer

You can add validation rules conditionally see https://laravel.com/docs/5.2/validation#conditionally-adding-rules

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement