问题
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