Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pierre4854's avatar

Validation with required_if and not_in not working

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 ?
</label>
</div>
</div>

<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>
    </select>
    @if($errors->has('state'))
        <div class="invalid-feedback">{{$errors->first('state')}}</div>
    @endif
</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 ?

Thanks.

0 likes
2 replies
ravasaurio's avatar

When you don't check a checkbox it wont be present on the request. Try with required_with instead of required_if:

'state' =>  'required_with:tocheck',

As for the not_in rule, it won't be necessary here: you can let the value on your 'Please Select' option as an empty string. Laravel will convert that to null with the ConvertEmptyStringsToNull middleware and since tocheck is checked, it will be present on the request so state is not allowed to be null. But just to clarify,your usage of the not_in rule is wrong. Here's how you use it:

You need to import

use Illuminate\Validation\Rule;

and use it like this:

'tocheck'  => 'sometimes',
 'state' =>  ['required', Rule::notIn(['Please Select', 'foo', 'bar'])],

Finally, I don't think you need the sometimes rule on your tocheck. The sometimes rule is intended to use with other rules. It is used when something is not required to be present, but needs to meet some requirements when it is present. So if you are OK with your tocheck not being on your request (when it is not checked) but you need it to be boolean, or numeric, or whatever when it is checked, then you use sometimes:

'tocheck'  => ['sometimes', Rule::in(['on'])], // It is not required, but if it's present, then it needs to be 'on' otherwise validation will fail

https://laravel.com/docs/5.8/validation#rule-required-with https://laravel.com/docs/5.8/validation#rule-not-in https://laravel.com/docs/5.8/validation#conditionally-adding-rules https://laravel.com/docs/5.8/validation#rule-in

AdhamLap's avatar

you use $errors->has('state')

check errors first after post request {{ $errors->all() }}

Please or to participate in this conversation.