Required_if laravel with multiple value

后端 未结 4 939
悲&欢浪女
悲&欢浪女 2020-12-16 10:33

I have a dropdown menu like this:


                        
    
提交评论

  • 2020-12-16 11:05

    I think that the require_if validation accept only one value per time. Try to change your validation code as below:

    $rule = array(
      'selection' => 'required',
      'stext' => 'required_if:selection,2|required_if:selection,3',
    );
    

    EDIT: Check LePhleg answer, is more cleaner. At the time of the answer that was not possible, just check the question, he was using the same method but not worked.

    0 讨论(0)
  • 2020-12-16 11:11

    try this one

    if single match value then used like that

    'stext'     => 'required_if:selection,2'
    

    if you have multiple value then used like that (separated by comma)

    'stext'     => 'required_if:selection,2,3'
    

    for more information see documentation required_if

    0 讨论(0)
  • 2020-12-16 11:20

    You can go ahead with sometimes validation for laravel. you can define a custom closure as in the below example

    public static myfunction(){
       $input = \Input::only('selection','stext');
       $rule = array(
          'selection' => 'required'
       );
    $validator->sometimes('stext', 'required', function($input){
        return (($input->selection == 1) || ($input->selection == 2));
    });
       $validate = \Validator::make($input,$rule);
    }
    
    0 讨论(0)
  • 提交回复
    热议问题