Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem validation when use When and Key #5

Open
GoldraK opened this issue Sep 1, 2018 · 1 comment
Open

Problem validation when use When and Key #5

GoldraK opened this issue Sep 1, 2018 · 1 comment

Comments

@GoldraK
Copy link

GoldraK commented Sep 1, 2018

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

@marcpeterson
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants