Validating 'other' field only if dropdown value is 'other'

不想你离开。 提交于 2019-12-11 06:39:22

问题


Slightly complicated one to explain, but a very common use case.

I have this field on my page:

<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'.

Here's my condensed validation code:

$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'.

How can I conditionally require the free text field, in Laravel 5.2?


回答1:


You're looking for the required_if rule:

'your_text_field' => 'required_if:how_you_heard_about_us,Other'


来源:https://stackoverflow.com/questions/48705355/validating-other-field-only-if-dropdown-value-is-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!