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

Validation with v::when not working #1470

Open
slashforward-nl opened this issue Nov 1, 2024 · 5 comments
Open

Validation with v::when not working #1470

slashforward-nl opened this issue Nov 1, 2024 · 5 comments

Comments

@slashforward-nl
Copy link

I have the following code:

try {
  $result = v::key(
    'textblok1', 
    v::when(
      v::key('radiobutton', v::equals('textblok1')),
      v::notEmpty()
    )
  )->assert($this->postData);
} catch (NestedValidationException $e) {
  print_r($e->getMessages());
}

$this->postData contains the following array:

Array
(
    [radiobutton] => textblok2
    [textblok1] => test
    [textblok2] => 
)

But i get the following error:
Array ( [textblok1] => "test" is not valid )

What am i doing wrong?

Thanks in advance!

Wouter

@andus4n
Copy link

andus4n commented Nov 2, 2024

when using nested conditions you're basically passing the previously checked value to the nested rules.
so, in your example, the engine is checking if "textblok1" is an array with the key "radiobutton" that should equal "textblok1", therefore it fails.
i'm guessin' you want somethin' like this:

v::when
(
   v::key('radiobutton', v::equals('textblok1')),
   v::key('textblok1', v::notEmpty()),
   v::alwaysValid()
)

@slashforward-nl
Copy link
Author

Thank you for your suggestion, too bad i get the same with this:

$result = v::key(
  'textblok1', 
  v::when(
    v::key('radiobutton', v::equals('textblok1')),
    v::key('textblok1', v::notEmpty())
  )
)->assert($this->postData);

Array
(
    [radiobutton] => textblok1
    [textblok1] => testtest
    [textblok2] => 
)

Array ( [textblok1] => "testtest" is not valid ) 

In this case radiobutton equals textblok1, so textblok1 field should not be empty. And its not in our case.
Any suggestions?

@andus4n
Copy link

andus4n commented Nov 2, 2024

Please read my comment thoroughly.
The proper validation rule for your scenario is:

v::when
(
   v::key('radiobutton', v::equals('textblok1')),
   v::key('textblok1', v::notEmpty()),
   v::alwaysValid() // assuming all other scenarios are acceptable
)
->assert($this->postData);

Of course, you should add extra rules like first validating that "postData" is an array etc etc

@slashforward-nl
Copy link
Author

Ah i thought i could do the v::when in a v::key so i could add rules based on the v::when.

Textbox rule (if radiobutton = textblok1) then notEmpty else alwaysValid.
Textbox2 rule (if radiobutton = textblok2) then notEmpty else alwaysValid.

I will check ik out, thanks for your help

@andus4n
Copy link

andus4n commented Nov 2, 2024

c0

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