Search code examples
phpvalidationlaravellaravel-4

Laravel Validation one of two fields must be filled


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

Solution

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