Skip to content
Advertisement

Required_if laravel with multiple value

I have a dropdown menu like this:

<select name="selection">
   <option value="1">Option 1</option>
   <option value="2">Option 2</option>
   <option value="3">Option 3</option>
</select>
<input type="text" name="stext">

I want the following in laravel:

public static myfunction(){
   $input = Input::only('selection','stext');
   $rule = array(
      'selection' => 'required',
      'stext' => 'required_if:selection,2,3',
   );
   $validate = Validator::make($input,$rule);
}

But if I select option 1, stext is still required. Why? How can I fix it?

Advertisement

Answer

EDIT: As pointed out by LePhleg answer, there is a nicest syntax for this validation:

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

Old answer below:

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement