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

tareenmj's avatar

Required only if a checkbox is not checked

I am a bit of a beginner in Laravel, and I had some trouble with validation. I want to make a field required only if the value of the a certain checkbox is 0. My rules currently are:

$rules=[
    'TB1_course.*' => 'required'
];

My checkbox input html is as such: " input type="hidden" value="0" name="TB1_checbox"

input type="checkbox" value="1" name="TB1_checkbox" //with the html tags of course "

I gave a hidden input just so that if the user does not select the checkbox, I receive a value of 0. My TB1_course is an array of inputs, since I used a clone function in jQuery.

Is there an easy way to make TB1_course required only if TB1_checkbox is 0?

Thank you for all your help!

0 likes
6 replies
Screenbeetle's avatar

If you use a custom form request then you should be able to just put the logic in there. Something like

public function rules()
{
        if(!isset($this->input('TB1_checkbox'))) {
        $rules=[
            'TB1_course.*' => 'required'
        ];
    }
}
1 like
tareenmj's avatar

@Screenbeetle can't I just do something like

$rules= [ 'TB1_course.*' => 'required_if: TB1_checkbox,0' ] ?

1 like
J5Dev's avatar
J5Dev
Best Answer
Level 11

Firstly, Im presuming you have some mechanism (js?) that allows for the value of the hidden input to be set to 1... otherwise it would always force the inputs to be required, and a simple required would do. That said...

The suggestion by Screenbeetle, whilst good is not correct. Sometimes is used when you need to chain a check to another, for example:

['username' => sometimes|email]

It's like a softer required, in that it doesn't force the key to be in the request, but if it is, it enforces the validation rules against it. Whereas required would fail because its there... essentially, sometimes is an 'its optional' approach.

The better option would be to use one of the required options.... I would say that in this scenario, the most descriptive is either required_without or required_if (one you suggested yourself)... both working the same way.

There are maybe better ways to handle things from both a UI and UX point of view, but would need more info about what is happening around the form :)

1 like
Screenbeetle's avatar

Evenin all

The better option would be to use one of the required options

Yep fair play, that suggestion is better - so long as you ensure you get back a 0 from the checkbox somehow. @J5Dev - am I right in thinking that switching to checking for a 1 instead of a 0 e.g. - 'TB1_course.*' => 'required_if: TB1_checkbox,1' ` - might illuminate the need for the hidden form field?

The suggestion by Screenbeetle, whilst good is not correct. Sometimes is used when you need to chain a check to another,

I could have explained it better but my point is correct still. I have a scenario where two slightly different forms are posting back to the same method - one with more fields than the other. My explanation maybe should have said ...

sometimes which just checks if the field exists [before it 'enforces the validation rules against it']

J5Dev's avatar

Yes, going for checking for a 1 would remove the ned for having a hidden field, but it also removes the ability to switch the requirement, thats why I mentioned a presumption of having a mechanism which set the hidden field according to some other event/status.

Having some more information would allow me to suggest something that may be better... It could be that neither approach is required, based on how the form is being put together... I noticed the checkboxes are added via js, so, it may better to only add them SHOULD they be needed, then simply set the to sometimes within the Laravel validation.

Please or to participate in this conversation.