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

arrayType + key validator + findMessages returns wrong messages #888

Closed
rauwebieten opened this issue Nov 22, 2017 · 2 comments
Closed

arrayType + key validator + findMessages returns wrong messages #888

rauwebieten opened this issue Nov 22, 2017 · 2 comments

Comments

@rauwebieten
Copy link

Is use dot syntax to find the messages I want from the exception, but when the name if the array key is the same as the rule-name, something is wrong.

My code:

<?php

namespace tests;

use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;

require_once '../vendor/autoload.php';

$data = [
    'email' => ''
];

$validator = v::arrayType()->key('email', v::stringType()->notEmpty()->email());

try {
    $validator->assert($data);
} catch (NestedValidationException $exception) {
    print_r($exception->findMessages(['email.notEmpty', 'email.email']));
}

This is the result:

Array
(
    [email_notEmpty] => email must not be empty
    [email_email] => These rules must pass for email
)

The email_email message is wrong, it does not return the "email" message for the "email" field.

But when I change the array key to "example_name" for instance, all is correct.

<?php

namespace tests;

use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;

require_once '../vendor/autoload.php';

$data = [
    'example_key' => ''
];

$validator = v::arrayType()->key('example_key', v::stringType()->notEmpty()->email());

try {
    $validator->assert($data);
} catch (NestedValidationException $exception) {
    print_r($exception->findMessages(['example_key.notEmpty', 'example_key.email']));
}

Output is now correct:

Array
(
    [example_key_notEmpty] => example_key must not be empty
    [example_key_email] => example_key must be valid email
)

I'm using v1.1.14

@mrliptontea
Copy link

I think I have a related issue to this on 1.1.19; Example:

$validator = v::create();
$validator->key('name', v::stringType()->length(4, 32));
$validator->key('red', v::intType()->between(0, 255));
$validator->key('green', v::intType()->between(0, 255));
$validator->key('blue', v::intType()->between(0, 255));

$validator->assert([
    'name' => 'red',
    'red' => 256,
    'green' => -1,
    'blue' => 0.0,
]);

I want to getFullMessage which is different from findMessages, but both rely on the iterator implementation.

Actual output

- All of the required rules must pass for { ... }
  - name must have a length between 4 and 32
    - red must be less than or equal to 255
    - green must be greater than or equal to 0
  - blue must be of the type integer

Notice how between rule gets the child validation's message instead of its own thus resulting in less clear message and wrong nesting level. Also, I get different results depending on which rules fail, even on different keys in the array.

Expected output

- All of the required rules must pass for { ... }
  - name must have a length between 4 and 32
  - red must be between 0 and 255
  - green must be between 0 and 255
  - blue must be of type integer

Now, the issue is over a year old. However, I'm getting the above expected output on master branch - any chance for a patch to 1.1 or a new release soon?

@henriquemoody
Copy link
Member

With so many open issues, it's become hard to really know what's really important and what I have fixed already.

I'm closing issues that are too old, and I'm not really looking at them all. If this is still relevant, please comment here and I will reopen this issue.

Thanks for reporting! 🐼

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

3 participants