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

mjp1's avatar
Level 2

Laravel Validate Array of Checkboxes Certain Values

Hey all. So, I know that in the documentation I should be able to use the "in:" validation condition to make sure the array of values I get from a set of checkboxes matches a certain list of values. However, in the browser, if I change the markup of the "value" attribute of the checkbox to be outside my list of acceptable values, the validation doesn't catch it and it posts to the database. Below is my code. Thanks for your help!

In my view:

<input type="checkbox" name="Type[]" value="A"/>
<input type="checkbox" name="Type[]" value="B"/>
<input type="checkbox" name="Type[]" value="C"/>

In my controller:

if ($request->ajax())
{
    /*Create record in UserType Model with the values from the Type checkboxes*/

    $Type = Input::get('Type');

    $this->validate($request, [
        'Type.*' => 'in:A,B,C,D',
    ]);

    foreach ($Type as $key => $value)
    {
        Auth::user()->userType()->create([
            'profile_id' => Auth::user()->id,
            'type' => $value,
        ]);
    }
}
0 likes
1 reply
mjp1's avatar
Level 2

Sorry all, the problem is because I was running Laravel 5.1 instead of 5.2 where this validation option is available.

Please or to participate in this conversation.