mardi 6 août 2019

Validatiion rule "between" for array of checkboxes always valid

If have a few questions displaying choice ids with ranking (First checkbox selected is first choice, etc). Here's a simplified var_dump($_POST) that was sent through the form:

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 as follow:

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

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

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

And finally var_dump($validationMessages) (however not so much important here):

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
)

This is all wrapped up like this:

$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

How to make the between:x,y validation rule work with an array of values?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire