83

I have two fields:

QQ

Email

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'),
);

4 Answers 4

167

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',
);
7
  • Not entirely sure. I rather think it's only email.required_without without the actual rule parameter, but you'll have to try out... Jan 10, 2015 at 11:11
  • It's without, which seems a little limiting :( Jan 10, 2015 at 11:34
  • Why is that limiting? You can only use the same rule once on the same field anyways? Jan 10, 2015 at 11:35
  • I want throw error only when both fields are absent. what should id do? It should work when both fields are present. May 10, 2018 at 12:08
  • I found that documentation a little odd. Thanks for pointing this out with an example. Jul 27, 2018 at 19:56
32

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.

1
  • This is a very good point. Making either field mandatory when the other is empty will trigger the error when both are missing. Adding a custom error message that does not reference the :attribute that triggered the validation failure will be important when doing this.
    – Jason
    May 12, 2022 at 15:06
7

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.