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

Different validation rules based on key value #45

Open
eko3alpha opened this issue Apr 4, 2019 · 1 comment
Open

Different validation rules based on key value #45

eko3alpha opened this issue Apr 4, 2019 · 1 comment
Labels

Comments

@eko3alpha
Copy link

eko3alpha commented Apr 4, 2019

It appears this validation middleware does not allow you to reference other $_POST values and create dynamic validation based on their values. Is there a way to do this?

For example, if a user selects NY as their state then I need them to input their email and license. Currently I'm doing it like this...

        $validators = [
            'state'   => v::subdivisionCode('US')->notOptional()
        ];

        $isStateNY = v::key('state', v::equals('NY'))->validate($_POST);

        if($isStateNY)
        {
            $validators['email']    = v::notOptional()->email();
            $validators['license'] =  v::notOptional();
        }

I tried doing this but it's not working.

        $validators = [
            'state'   => v::subdivisionCode('US')->notOptional(),
            'email'   => v::when(
                v::key('state', v::equals('NY')),
                v::notOptional()->noWhitespace(),
                v::alwaysValid()
            ),
            'license'  => v::when(
                v::key('state', v::equals('NY')),
                v::notOptional()->noWhitespace(),
                v::alwaysValid()
            )
        ];

I would like to pass in the Respect object without having to manually check the $_POST['state'] value. Can I format the $validator in a way that can reference other keys?

Here is the Respect way of doing it.

        $_POST = [
            'state' => 'NY',
            'email' => '[email protected]',
            'license' => 'MIT'
        ];

        $result = v::key('state', v::subdivisionCode('US')->notOptional())
        ->when(
            v::key('state', v::equals('NY')),            // if state = NY
            v::key('email', v::notOptional()->email()),  // then add validation to email
            v::alwaysValid()                             // else email is always valid
        )
        ->when(
            v::key('state', v::equals('NY')),            // if state = NY
            v::key('license',  v::notOptional()),        // then make license required
            v::alwaysValid()                             // else license is always valid
        ) 
        ->validate($_POST);
@DavidePastore
Copy link
Owner

DavidePastore commented Jun 22, 2019

Hi @eko3alpha . Sorry for the late response. You can try the version that is under development here: #40. It does support your use case and you can also see some green tests based on your issue: 15de8fd
Please, let me know if it works for you.

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

No branches or pull requests

2 participants