Try to use the getRelatedByName() method first

Then using the `findMessages()` method, tries to use the method
`getRelatedByName()` before using the `findRelated()` method.

This change was made because since on `KeyNested` rule you may have
names with '.' we'll never get the proper exception using
`findRelated()`.
This commit is contained in:
Henrique Moody 2016-09-17 20:49:33 +02:00
parent 98443bad0c
commit 4bcc4027f8
No known key found for this signature in database
GPG key ID: 221E9281655813A6
2 changed files with 60 additions and 1 deletions

View file

@ -68,7 +68,9 @@ class NestedValidationException extends ValidationException implements IteratorA
$numericKey = is_numeric($key);
$path = $numericKey ? $value : $key;
$exception = $this->findRelated($path);
if (!($exception = $this->getRelatedByName($path))) {
$exception = $this->findRelated($path);
}
$path = str_replace('.', '_', $path);

View file

@ -0,0 +1,57 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\NestedValidationException;
use Respect\Validation\Validator as v;
$input = [
'organization' => [
'name' => 'N',
'address' => 'A',
],
'contact' => [
'name' => 'wd',
'email' => 'ffesfewf2232313123212',
'password' => ' ',
'position' => 'wdwdwf',
'number' => '£"!@£;21#£:"!:£~!":£\'21;',
],
];
try {
v::create()
->key(
'organization',
v::create()
->key('name', v::length(2, 50)->notEmpty())
->key('address', v::length(2, 50)->notEmpty())
)
->keyNested('contact.name', v::length(1, 50)->notEmpty())
->keyNested('contact.email', v::email()->notEmpty())
->keyNested('contact.password', v::length(3, 100)->notEmpty())
->keyNested('contact.position', v::length(1, 100)->notEmpty())
->keyNested('contact.number', v::phone()->notEmpty())
->assert($input);
} catch (NestedValidationException $exception) {
print_r(array_filter($exception->findMessages([
'organization.name' => 'Center name',
'organization.address' => 'Center address',
'contact.name' => 'Contact name',
'contact.password' => 'Contact name',
'contact.position' => 'Contact name',
'contact.number' => 'Contact name',
'contact.email' => 'Contact name',
])));
}
?>
--EXPECTF--
Array
(
[organization_name] => Center name
[organization_address] => Center address
[contact_password] => Contact name
[contact_number] => Contact name
[contact_email] => Contact name
)