ajax_27 ajax_27 - 2 months ago 11
PHP Question

How can I validate my Other option in dropdown list?

How can I validate my Other input field in Laravel 4.2 which appears after choosing Other option in a dropdown list? The values from my dropdown list are already required, now I want to apply the same loggic of validation with errors on my input field so that both are required! By fulfilling the first validation rule from the dropdown list the second validation rule from the input field ouputs the error! Whats the easiest way to solve this!

<div class="row">

<div class="col-lg-6 col-sm-6 col-md-6">



<script type="text/javascript">
function showfield(name){
if(name=='Other')document.getElementById('div1').innerHTML='<input class="custom-input" placeholder="Roles" style="margin-left: 32px;" name="roles" type="text" value/>';

else document.getElementById('div1').innerHTML='';
}
</script>



<select id="otherField" name="role" onchange="showfield(this.options[this.selectedIndex].value)">
<option value="" selected="selected">Select role</option>
<option value="UI/UX designers">UI/UX designers</option>
<option value="QA engineers">QA engineers</option>
<option value="Developer">Developer</option>
<option value="Scrum Master">Scrum Master</option>
<option value="CEO">CEO</option>
<option value="Director">Director</option>
<option value="Other">Other</option>
</select>
<div id="div1" class="row">
<div class="col-lg-6 col-sm-6 col-md-6"></div>
</div>
<div class="col-md-6">
@if($errors->first('roles') )
<div class="fe-form-error">
<div class="icon"></div>
{{ $errors->first('roles') }}
</div>
@endif
</div>

</div>

<div class="col-md-6">
@if($errors->first('role') )
<div class="fe-form-error">
<div class="icon"></div>
{{ $errors->first('role') }}
</div>
@endif
</div>

</div>


My class with validation rules:

class OnEventRegistration extends AbstractValidator {

protected $customRules = false;

protected $rules = array(
'email' => 'required|email|unique:users',
'password' => 'required|min:8|confirmed',
'first_name' => 'required',
'last_name' => 'required',
'role' => 'required',
'roles' => 'required',

);
}

Answer

You need to use required_if.

protected $rules = array(
    'email'             =>      'required|email|unique:users',
    'password'          =>      'required|min:8|confirmed',
    'first_name'        =>      'required',
    'last_name'         =>      'required',
    'role'              =>      'required',
    // This says required only if role = 'Other'
    'roles'             =>      'required_if:role,Other',
);

You'll likely also need another method of conditionally showing your roles input so that it's actually recognized as part of the DOM within your form.

<script>
function showfield(el) {
if (el.value == 'Other') {
    document.getElementById('roles').style.display = 'block';
} else {
    document.getElementById('roles').style.display = 'none';
}
</script>

<select id="otherField" name="role" onchange="showfield(this)">
    <!-- options -->
</select>

<input id="roles" style="display: none; margin-left: 32px;" name="roles" type="text" placeholder="Roles" class="custom-input">