1
votes

i have one form which has 4 text fields. I want Laravel to check if the fields are field, but it does not need to check all of them, just only two of them.

Example: field1, field2, field3, field4. To complete the form the user must enter values for field1 and field2 or for field3 and field4.

Is this possible ?

1
You could use a custom validation rule, this allows you to provide a complex function for field validation instead of the simple rules. You can find some infos here : laravel.com/docs/5.1/validation#custom-validation-ruleskerwan
yes, i know the ability to use custom validation rule, but i want to know if the default rules can help me ?Vince Carter
you can use required_with:foo,bar,...codeGig
I don't think this is possible with the standard rules, except if you extend the validator with extra rules, like this example : gist.github.com/juice49/4176954kerwan
@Jitendra thanks for the required_with rule, I wonder how i never noticed this one ! OP : You can also check this example which seems to adress a similar problem stackoverflow.com/questions/23401365/…. You could require field_1 or field_3 to be filled and then condition field_2 to field_1 and field_4 to field_3 using a combination of "required_with" and "required_without_all". I hope this helps !kerwan

1 Answers

1
votes

i will post the answer if somebody needs it in future. I have one field (price), which is always required. Than i have three additional fields where the combination field (volume) or fields (buy, sell) is also required.

Here is the solution:

return [
    'price' => ['required'],
    'volume' => ['required_without_all:buy,sell'],
    'buy' => ['required_without:volume'],
    'sell' => ['required_without:volume'],
];