5 months ago

Validation with required_if and not_in not working

Posted 5 months ago by pierre4854

Hi guys,

I'm trying to set my own validation and I want to required a field if a checkbox is checked. Here is my HTML :

<div class="form-group col-md-6 col-xs-12">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="tocheck" name="tocheck" {{ old('tocheck') == 'on' ? 'checked' : '' }} value="on">
<label class="form-check-label" for="tocheck">
Check me ?

<div class="form-group col-md-6 col-xs-12">
    <label for="state">State:</label>
    <select class="form-control @if($errors->has('state')) is-invalid @endif" id="state" name="state">
        <option class="default disabled selected">Please Select</option>
        <option value="AL" {{ old('state') == 'AL' ? 'selected' : '' }}>Alabama</option>
        <option value="AK" {{ old('state') == 'AK' ? 'selected' : '' }}>Alaska</option>
        <option value="AZ" {{ old('state') == 'AZ' ? 'selected' : '' }}>Arizona</option>
        <div class="invalid-feedback">{{$errors->first('state')}}</div>

Now, I've this validation rule :

    public function rules()
        return [
                'tocheck'  => 'sometimes',
        'state' =>  'required_if:tocheck,on|not_in:Please Select',

Now even I'm not checking the checkbox, I'm getting an error on the select field. Did I miss something ? Or what's wrong ?


Please sign in or create an account to participate in this conversation.