awurth/SlimValidation

Problem validation when use When and Key

Opened this issue · 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

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.