Reputation: 85
in my form i let the user select two countries, now the two countries must not be empty means the user have to select two in two diferent selects and one of the fields must be lets say "england" if field1 is england than the field2 must be required but it's not important if it's equal to "england", if field1 is not equal to "england" than field2 must be required and equal to "england" ?
something like this i think:
$rules =[
'field1' => '', //if this equals 'england' the field2 is just required
'field2' => '' // if field1 is different from 'england' than this field must equal 'england'
];
any idea how i can do this with laravel rules?
Upvotes: 1
Views: 692
Reputation: 1743
I think you dont need a rule for this situation.
You can do like this, (write under $this->validate($rule)
):
if ($request->field1 == 'england' && empty($request->field2))
return 'Field2 required when field1 is england';
if ($request->field1 != 'england' && $request->field2 != 'england')
return 'Field2 must be "england" when field1 different from "england"';
There is a custom validation in laravel but i think it works for 1 parameter. https://laravel.com/docs/5.7/validation#custom-validation-rules
Upvotes: 1