0

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

    );
}
ajax_27
  • 63
  • 7

1 Answers1

1

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">
tptcat
  • 3,894
  • 2
  • 32
  • 51
  • Tried already I get a **SQLSTATE[23000]**: Integrity constraint violation: 1048 Column 'roles' cannot be null – ajax_27 Oct 02 '16 at 14:24
  • That's not at all related to any of the above code, but to solve this you need to change the `roles` column in your database to allow for null. If this was created with a migration then: `$table->string('roles')->nullable();` or in SQL: http://stackoverflow.com/questions/212939/how-do-i-modify-a-mysql-column-to-allow-null In MySQL columns are nullable by default. In Laravel using the built-in migrations, you need to specify which are nullable. – tptcat Oct 02 '16 at 14:30
  • Working!!Thanks a lot for your effort! Appreciate it. – ajax_27 Oct 02 '16 at 14:42
  • Glad I could help. – tptcat Oct 02 '16 at 14:46