Reputation: 24122
I have two fields:
How do I set up a Validator object so that one of these fields must be filled? It doesn't matter which.
$messages = array(
'email.required_without:qq' => Lang::get('messages.mustenteremail'),
'email.email' => Lang::get('messages.emailinvalid'),
'qq.required_without:email' => Lang::get('messages.mustenterqq'),
);
Upvotes: 90
Views: 61116
Reputation: 49
Laravel >= 10.10
Try this
public function rules(): array
{
return [
'email' => 'required_without:phone_number|email|exists:customers,email',
'phone_number' => 'required_without:email|digits_between:10,11|exists:customers,phone_number',
];
}
Upvotes: 0
Reputation: 1769
One more point that I have searched alot. But I have figured out like this.
$rules['husband_name'] = 'required_if:gender,==,female && marital_status,==,married|string|max:255';
In my case, it's like gender must be female
and marital_status must be married
then husband name is required.
Upvotes: 0
Reputation: 391
Laravel >= 8.32 only support.
Both (mobile or email) are presented simultaneously -> throw an error.
Both (mobile or email) is not present simultaneously -> throw an error.
Allowed
Only one parameter can be allowed.
'email' => [ 'prohibited_unless:mobile,null,','required_without:mobile','email', 'max:255', ],
'mobile' => [ 'prohibited_unless:email,null','required_without:email', 'digits_between:5,13', 'numeric' ],
EDIT: You can also use prohibits:email
and prohibits:mobile
in combination of required_without:...
rule
Upvotes: 10
Reputation: 5499
In the example above (given by lucasgeiter) you actually only need one of the conditions not both.
$rules = array(
'Email' => 'required_without:QQ'
);
If you have both rules then filling neither in will result in TWO error messages being displayed. This way it will check that at least one field is filled in and will only display one error message if neither are filled.
Upvotes: 33
Reputation: 152890
required_without
should work.
It means that the field is required if the other field is not present. If have more than two fields and only one is required, use required_without_all:foo,bar,...
$rules = array(
'Email' => 'required_without:QQ',
'QQ' => 'required_without:Email',
);
Upvotes: 178