Required_if laravel具有多个值 [英] Required_if laravel with multiple value

查看:295
本文介绍了Required_if laravel具有多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的下拉菜单:

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">

我想在laravel中找到以下内容:

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);
}

但是,如果我选择选项1,则仍然需要stext.为什么? 我该如何解决?

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

推荐答案

我认为require_if验证每次仅接受一个值. 尝试如下更改您的验证码:

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

检查LePhleg的答案,比较干净. 在无法回答的时候,只需检查问题,他正在使用相同的方法但没有用.

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.

这篇关于Required_if laravel具有多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆