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

artisticre's avatar

Checkbox

I am dealing with a three option checkbox. In the validator, do I do it as boolean?

'checkbox' => ['required', 'boolean', 'max:255'],

and in the store function how do I input it?

0 likes
10 replies
Nakov's avatar

@artisticre

A boolean has a true and false values, so what do you mean with three options? You should have a different check then if the value is different.

jlrdw's avatar

'checkbox' => ['required', 'boolean', 'max:255'],

Shouldn't you be storing a 1 or 0 for a tinyint. And if not checked nothing is passed.

So by required, are you saying has to be checked.

1 like
artisticre's avatar

I have a checkbox named services and then the options would be service 1, service 2, service 3. Any can be clicked or none can be clicked. I don't understand how to put it in validation and input into the database.

jove's avatar

@artisticre Do you plan to store more than 1, like an array? [1,2]? If not you should probably use radio instead of checkboxes. If you want to store multiple you should add [] after the name to make it an array and the validation rule would be something like this:

'services.*' => ['in:1,2,3'],

If I'm wrong about the [] please correct me anyone.

artisticre's avatar

Yes I am wanting to store multiple options or none, depending on what they choose. So if I add this rule in the validator, do I add the array to the input on the form as well? Such as

name="services[]"
Cronix's avatar

Rules should just be

'checkbox' => ['boolean'],

boolean is true/false or 0/1. No point in also saying max:255 when it can already be only a 0/1.

To get value, just

$value = $request->has('checkbox');

will be a 1 if they checked it, or a 0 if they didn't (due to has() returning a boolean)

artisticre's avatar

But if there are multiple checkboxes, it wouldn't be checked or not checked?

jlrdw's avatar

If this is a checkbox array, you have to Loop over it and see what was checked and not checked.

In other words the values that are in the loop were checked.

Please or to participate in this conversation.