仅当下拉值为“其他"时才验证“其他"字段 [英] Validating 'other' field only if dropdown value is 'other'
本文介绍了仅当下拉值为“其他"时才验证“其他"字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一个稍微复杂的解释,但一个非常普通的用例.
Slightly complicated one to explain, but a very common use case.
我的页面上有此字段:
<select class="form-control" name="how_you_heard_about_us" id="select-hearaboutus">
其中的所有选项都有名称,包括以下名称:
All of the options within it have names, including this one:
<option value="Other">Other</option>
我还有一个文本输入,只有在用户选择其他"时才需要.
I also have a text input, which is only required if the user selects 'Other'.
这是我的简明验证码:
$this->validate($request, [
'first_name' => 'required|max:20',
'last_name' => 'required|max:20',
'how_you_heard_about_us' => 'required'
]);
我正在尝试进行设置,因此,如果下拉菜单设置为其他",则只需要用户输入文本字段即可.
I'm trying to set it up, so that the user is only required to enter into the text field, if the dropdown is set to 'other'.
如何在Laravel 5.2中有条件地要求使用自由文本字段?
How can I conditionally require the free text field, in Laravel 5.2?
推荐答案
您正在寻找 required_if
规则:
You're looking for the required_if
rule:
'your_text_field' => 'required_if:how_you_heard_about_us,Other'
这篇关于仅当下拉值为“其他"时才验证“其他"字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文