Laravel Validation one of two fields must be filled

前端 未结 2 383
孤城傲影
孤城傲影 2020-12-29 18:09

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.




        
相关标签:
2条回答
  • 2020-12-29 18:46

    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',
    );
    
    0 讨论(0)
  • 2020-12-29 18:48

    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.

    0 讨论(0)
提交回复
热议问题