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

Question: RequiredWithout with Optional #317

Open
SplitterAlex opened this issue Jul 28, 2020 · 1 comment
Open

Question: RequiredWithout with Optional #317

SplitterAlex opened this issue Jul 28, 2020 · 1 comment

Comments

@SplitterAlex
Copy link

Hi!

I can't get my head around a validation I am trying to archive.

Let's take the example from the README where you can conditionally require values using required conditional rules:

// this rule set would work for either data set...
$data = ['email' => 'test@test.com', 'password' => 'mypassword'];
// or...
$data = ['token' => 'jashdjahs83rufh89y38h38h'];

$v = new Valitron\Validator($data);
$v->rules([
    'requiredWithout' => [
        ['token', ['email', 'password'], true]
    ],
    'requiredWith' => [
        ['password', ['email']]
    ],
    'email' => [
        ['email']
    ],
    'optional' => [
        ['email']
    ]
]);
$this->assertTrue($v->validate());

Lets say I want to add a rule to the token entry. For example I want to check if the token only contains AlphaNum values.
I would do it like this:

// this rule set would work for either data set...
$data = ['email' => 'test@test.com', 'password' => 'mypassword'];
// or...
$data = ['token' => 'jashdjahs83rufh89y38h38h'];

$v = new Valitron\Validator($data);
$v->rules([
    'requiredWithout' => [
        ['token', ['email', 'password'], true]
    ],
    'requiredWith' => [
        ['password', ['email']]
    ],
    'alphaNum' => [
       ['token']
    ],
    'email' => [
        ['email']
    ],
    'optional' => [
        ['email', 'token']
    ]
]);
$this->assertTrue($v->validate());

I add the rule 'alphaNum' to check if the token only contains alphanumerical values. I also add the 'token' to the 'optional' rule, because my intention is to check the token only if its exists.

But when I run the above code with 'email' and 'token' as data I always get the following error:

array(1) {
  ["token"]=>
  array(1) {
    [0]=>
    string(54) "Token must contain only letters a-z and/or numbers 0-9"
  }
}

Am I doing something wrong or is this kind of validation not possible?

Thank you for your help!

Alex

@SplitterAlex
Copy link
Author

Possible related to #304

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

1 participant