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

randm's avatar

How to I validate checkboxs in a form?

Hi,

I am trying to validate a form using Laravel 5.1.

One of the forms has multiple checkboxes. Which validating rule would I use to validate returned array? The values in the returned array must be integer >= 1.

Also, when I redirect the user back to the form using withInput, the checkbox are not auto filled where the rest are. how can I fix this?

0 likes
15 replies
randm's avatar

@jillztom Thank you for your feedback. I check that page before asking this queston. I still don't know what to use. I am using array but that does not allow me to check the values with in it. also, I am not sure why when I am redirected to the form the checkboxes are not checked.

JillzTom's avatar

To check the checkbox when you come back to the page, you have to check a condition like below.

<input type="checkbox" value="orange" name="fruit" @if(Input::old('fruit')=="orange" checked @endif)>
randm's avatar

@jillztom Thank you for that. that worked for the radio buttons but it is not working for the check the name of my check box will be fruit[]

randm's avatar

here you go

                <label class="checkbox-inline" for="item_1">
                    <input name="control_4[]" id="item_1" value="1" type="checkbox"> A
                </label>

                <label class="checkbox-inline" for="item_2">
                    <input name="control_4[]" id="item_2" value="2" type="checkbox"> B
                </label>
                <label class="checkbox-inline" for="item_3">
                    <input name="control_4[]" id="item_3" value="3" type="checkbox"> C
                </label>
JillzTom's avatar

Try that without the [] when you check the condition in if statement.

randm's avatar

@jillztom still not working.

I tried to dd the input::old, and I get this

control_3 = "Blah Blah",
control_4 = array (0 => 1, 1 =>2)
randm's avatar

Okay I got the check box to be set on the error like this

{{ ( is_array( Input::old('fruit')) && in_array('orange', Input::old('fruit') ) ) ? 'checked' : '' }}

But I am still struggling on how to validate the array. I tried this validation rule but it is not working

integer|array|min:1
JillzTom's avatar

You can use min:value to validate a numeric value and you can also use it to validate an array's size.

Validator::make( 
    [ 'fruits' => Input::get('fruits') ],
    [ 'fruits' => 'min:1' ]
);
$validator = Validator::make([
    'fruits' => ['Orange', 'Apple', 'Grapes']
    ], ['fruits' => 'min: 4']);

$result = $validator->fails(); // returns true
randm's avatar

@jillztom I am not looking for the size of the array. What I want to make sure that every value in the array is > 0 "unless the field is not required then empty will pass too"

Snapey's avatar

You need to iterate over the array of inputs and add a rule to the rules array for each checkbox.

coffee's avatar
public function rules()
    {
        //validating input text field named "owner"
        $rules = ['owner' => 'required|max:15',];

        //validating array of input text fields named "item"
        foreach($this->request->get('item') as $a => $z)
        {
            $rules['item.'.$a] = 'required';
        }

        //validating array of  checkbox named "fruit"
        foreach($this->request->get('fruit') as $b => $y)
        {
            $rules['fruit.'.$b] = 'required';
        }


        return $rules;
    }

Thissite will help

johnatan.cunha's avatar

public function rules()
{
    return [
        'password' => 'required_if:password_checkbox,on|min:6|confirmed',
        'password_confirmation'   =>  'required_if:password_checkbox,on|min:6|same:password'
    ];
}

Please or to participate in this conversation.