SlimValidation icon indicating copy to clipboard operation
SlimValidation copied to clipboard

Problem validation when use When and Key

Open GoldraK opened this issue 7 years ago • 1 comments

Hi, first thank for the help.

I have the following data array:

$ values ​​["name"] = "second order";
$ values ​​["use_building"] = 8;
$ values ​​["other_use_building"] = "cave";

The validation has to check that when use_building is 8 the other_use_building is not empty.

If I perform the validation in a file for tests, the following validation works correctly:

$buildingValidator = v::key('use_building', v::optional(v::intVal()))
    ->when(v::key('use_building', v::equals(8)),
        v::key('other_use_building', v::notEmpty()),
        v::key('use_building', v::optional(v::intVal()))
    );

var_dump($buildingValidator->validate($values));

But when I enter it in the slim-validation system

return $this->validator->validate($values,
            [
                'name'    => ['rules' => V::notEmpty(),
                    'messages' => [
                        'notEmpty' => 'The name can not be left blank',
                    ]
                ],
                'use_building' => ['rules' => V::optional(V::intVal()->between(1, 8)),
                    'messages' => [
                        'intVal' => 'IT is not a valid type of use',
                        'between' => 'It is not a permitted use',
                    ]
                ],
                'other_use_building' => [ 'rules' => V::key('use_building', V::optional(V::intVal()->between(1, 8)))
                    ->when(V::key('use_building', V::equals(8)),
                        V::key('other_use_building', V::notEmpty()),
                        V::key('use_building', V::optional(v::intVal()))
                    ),
                    'messages' => [
                        'notEmpty' => 'The Other user buildin can not be left blank',
                    ]
                ]
            ]);

tells me what error when he had to see given true

{
    "errors": {
        "other_use_building": {
            "key": "Key use_building must be present"
        }
    }
}

Thanks for the help

GoldraK avatar Sep 01 '18 04:09 GoldraK

Did you ever solve this? I'm having the same problem. It seems like when() doesn't get the full array for key() to get data from.

marcpeterson avatar Jan 13 '22 00:01 marcpeterson