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

NaturalBornCamper's avatar

Validation rule “between” for array of checkboxes always valid

Hello all!

I'm need validation for a question of the style: "Select your interests in order of preference in the following checkboxes".

I've got the frontend and php all figured out but can't make the validation work. if I var_dump($_POST) I get the following:

Array
(
    [_token] => 6vPgIM1MX7MJ0DzMX2OFjCdPEBBel5mRJ6RzrEjR
    [11] => Array // Means question id 11
        (
            [42] => 2 // Means choice id 42 was chosen second
            [43] => 1 // Means choice id 43 was chosen first
            [44] => 3 // Means choice id 44 was chosen third
        )

    [12] => 

    [13] => Array
        (
            [4] => 1
        )

    [btn-validate] => CONTINUE
)

Then my var_dump($validationRules) is like this (simplified, only 3 questions):

Array
(
    [11] => Array
        (
            [0] => between:0,2
        )

    [12] => Array
        (
            [0] => between:1,3
        )

    [13] => Array
        (
            [0] => between:0,1
        )
)

And finally, my var_dump($validationMessages) yields this:

Array
(
    [11.between] => You should select between 0 and 2 choices
    [12.between] => You should select between 1 and 3 choices
    [13.between] => You should select between 0 and 1 choices
)

However when I try validating the input, it's valid with the previous dumps (and is still valid for pretty much anything I put send):

$validator = Validator::make($request->all(), $validationRules, $validationMessages);

if ($validator->fails()) {
    var_dump($validator->errors());
    exit('fail'); // Never ends up here
}
exit('all good'); // Always ends up here

Am I missing something? How to make the between:x,y validation rule work with an array of values that needs between X and Y selected checkboxes?

Thanks! :)

0 likes
4 replies
NaturalBornCamper's avatar

Ah, you're right! I thought the * was not needed for that particular purpose..

For anyone having the same problem, I also had to change the messages:

$messages: Array
(
    [11.*.between] => You should select between 0 and 2 choices
    [12.*.between] => You should select between 2 and 3 choices
    [13.*.between] => You should select between 0 and 1 choices
)

However, there is problem with the errors, the message is associated with the specific choice instead of the question itself:

$validator->errors(): Illuminate\Support\MessageBag Object
(
    [messages:protected] => Array
        (
            [12.48] => Array
                (
                    [0] => You should select between 2 and 3 choices
                )

        )
)

So I guess I will go with my the other idea I had and make a custom validator.. :/

NaturalBornCamper's avatar

Actually, scratch that, this seems incorrect as well, It's actually checking if the length of each answer is between 2 and 3 characters long. If I select 2 answers for question 12, I still get validation errors:

$validator->errors(): Illuminate\Support\MessageBag Object
(
    [messages:protected] => Array
        (
            [12.48] => Array
                (
                    [0] => You should select between 2 and 3 choices
                )

            [12.49] => Array
                (
                    [0] => You should select between 2 and 3 choices
                )

        )
)

Towards custom validation then..

Please or to participate in this conversation.