mirror of
https://github.com/Respect/Validation.git
synced 2024-06-29 10:40:24 +02:00
Remove findMessages() from NestedValidationException
This commit is contained in:
parent
9c81498395
commit
64ec329663
|
@ -141,7 +141,6 @@ $usernameValidator->validate('#$%'); //false
|
||||||
- Extends the `Respect\Validation\Exceptions\ValidationException` class
|
- Extends the `Respect\Validation\Exceptions\ValidationException` class
|
||||||
- Usually thrown when the `assert()` fails
|
- Usually thrown when the `assert()` fails
|
||||||
- Available methods:
|
- Available methods:
|
||||||
- `findMessages()`;
|
|
||||||
- `getFullMessage()`;
|
- `getFullMessage()`;
|
||||||
- `getMessages()`;
|
- `getMessages()`;
|
||||||
- more...
|
- more...
|
||||||
|
@ -196,46 +195,6 @@ Array
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Getting messages as an array by name
|
|
||||||
|
|
||||||
If you want to get specific message by name you can use `findMessages()` passing
|
|
||||||
the names of the rules you want:
|
|
||||||
|
|
||||||
```php
|
|
||||||
try {
|
|
||||||
$usernameValidator->assert('really messed up screen#name');
|
|
||||||
} catch(NestedValidationException $exception) {
|
|
||||||
print_r($exception->findMessages(['alnum', 'noWhitespace']));
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
The `findMessages()` returns an array with messages from the requested validators,
|
|
||||||
like this:
|
|
||||||
|
|
||||||
```no-highlight
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[alnum] => "really messed up screen#name" must contain only letters (a-z) and digits (0-9)
|
|
||||||
[noWhitespace] => "really messed up screen#name" must not contain whitespace
|
|
||||||
)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Custom messages
|
|
||||||
|
|
||||||
Getting messages as an array is fine, but sometimes you need to customize them in order
|
|
||||||
to present them to the user. This is possible using the `findMessages()` method as well:
|
|
||||||
|
|
||||||
```php
|
|
||||||
$errors = $exception->findMessages([
|
|
||||||
'alnum' => '{{name}} must contain only letters and digits',
|
|
||||||
'length' => '{{name}} must not have more than 15 chars',
|
|
||||||
'noWhitespace' => '{{name}} cannot contain spaces'
|
|
||||||
]);
|
|
||||||
```
|
|
||||||
|
|
||||||
For all messages, the `{{name}}` variable is available for templates. If you
|
|
||||||
do not define a name it uses the input to replace this placeholder.
|
|
||||||
|
|
||||||
## Message localization
|
## Message localization
|
||||||
|
|
||||||
You're also able to translate your message to another language with Validation.
|
You're also able to translate your message to another language with Validation.
|
||||||
|
|
|
@ -36,80 +36,6 @@ class NestedValidationException extends ValidationException implements IteratorA
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $path
|
|
||||||
* @param ValidationException $exception
|
|
||||||
*
|
|
||||||
* @return ValidationException
|
|
||||||
*/
|
|
||||||
private function getExceptionForPath($path, ValidationException $exception)
|
|
||||||
{
|
|
||||||
if ($path === $exception->guessId()) {
|
|
||||||
return $exception;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$exception instanceof self) {
|
|
||||||
return $exception;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($exception as $subException) {
|
|
||||||
return $subException;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $exception;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $paths
|
|
||||||
*
|
|
||||||
* @return string[]
|
|
||||||
*/
|
|
||||||
public function findMessages(array $paths)
|
|
||||||
{
|
|
||||||
$messages = [];
|
|
||||||
|
|
||||||
foreach ($paths as $key => $value) {
|
|
||||||
$numericKey = is_numeric($key);
|
|
||||||
$path = $numericKey ? $value : $key;
|
|
||||||
|
|
||||||
if (!($exception = $this->getRelatedByName($path))) {
|
|
||||||
$exception = $this->findRelated($path);
|
|
||||||
}
|
|
||||||
|
|
||||||
$path = str_replace('.', '_', $path);
|
|
||||||
|
|
||||||
if (!$exception) {
|
|
||||||
$messages[$path] = '';
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$exception = $this->getExceptionForPath($path, $exception);
|
|
||||||
if (!$numericKey) {
|
|
||||||
$exception->setTemplate($value);
|
|
||||||
}
|
|
||||||
|
|
||||||
$messages[$path] = $exception->getMainMessage();
|
|
||||||
}
|
|
||||||
|
|
||||||
return $messages;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Exception
|
|
||||||
*/
|
|
||||||
public function findRelated($path)
|
|
||||||
{
|
|
||||||
$target = $this;
|
|
||||||
$pieces = explode('.', $path);
|
|
||||||
|
|
||||||
while (!empty($pieces) && $target) {
|
|
||||||
$piece = array_shift($pieces);
|
|
||||||
$target = $target->getRelatedByName($piece);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $target;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return RecursiveIteratorIterator
|
* @return RecursiveIteratorIterator
|
||||||
*/
|
*/
|
||||||
|
@ -263,30 +189,6 @@ class NestedValidationException extends ValidationException implements IteratorA
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
private function isRelated($name, ValidationException $exception)
|
|
||||||
{
|
|
||||||
return $exception->getId() === $name || $exception->getName() === $name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return ValidationException
|
|
||||||
*/
|
|
||||||
public function getRelatedByName($name)
|
|
||||||
{
|
|
||||||
if ($this->isRelated($name, $this)) {
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($this->getRecursiveIterator() as $exception) {
|
|
||||||
if ($this->isRelated($name, $exception)) {
|
|
||||||
return $exception;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $exceptions
|
* @param array $exceptions
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,51 +0,0 @@
|
||||||
--TEST--
|
|
||||||
findMessages() should apply templates to flattened messages
|
|
||||||
--FILE--
|
|
||||||
<?php
|
|
||||||
require 'vendor/autoload.php';
|
|
||||||
|
|
||||||
use Respect\Validation\Exceptions\NestedValidationException;
|
|
||||||
use Respect\Validation\Validator as v;
|
|
||||||
|
|
||||||
$stringMax256 = v::stringType()->length(5, 256);
|
|
||||||
$alnumDot = v::alnum('.');
|
|
||||||
$stringMin8 = v::stringType()->length(8, null);
|
|
||||||
$validator = v::allOf(
|
|
||||||
v::attribute('first_name', $stringMax256)->setName('First Name'),
|
|
||||||
v::attribute('last_name', $stringMax256)->setName('Last Name'),
|
|
||||||
v::attribute('desired_login', $alnumDot)->setName('Desired Login'),
|
|
||||||
v::attribute('password', $stringMin8)->setName('Password'),
|
|
||||||
v::attribute('password_confirmation', $stringMin8)->setName('Password Confirmation'),
|
|
||||||
v::attribute('stay_signedin', v::notEmpty())->setName('Stay signed in'),
|
|
||||||
v::attribute('enable_webhistory', v::notEmpty())->setName('Enabled Web History'),
|
|
||||||
v::attribute('security_question', $stringMax256)->setName('Security Question')
|
|
||||||
)->setName('Validation Form');
|
|
||||||
try {
|
|
||||||
$validator->assert(
|
|
||||||
(object) [
|
|
||||||
'first_name' => 'fiif',
|
|
||||||
'last_name' => null,
|
|
||||||
'desired_login' => null,
|
|
||||||
'password' => null,
|
|
||||||
'password_confirmation' => null,
|
|
||||||
'stay_signedin' => null,
|
|
||||||
'enable_webhistory' => null,
|
|
||||||
'security_question' => null,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
} catch (NestedValidationException $e) {
|
|
||||||
$messages = $e->findMessages(
|
|
||||||
[
|
|
||||||
'allOf' => 'Invalid {{name}}',
|
|
||||||
'first_name.length' => 'Invalid length for {{name}} {{input}}',
|
|
||||||
]
|
|
||||||
);
|
|
||||||
print_r($messages);
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
--EXPECTF--
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[allOf] => Invalid Validation Form
|
|
||||||
[first_name_length] => Invalid length for first_name "fiif"
|
|
||||||
)
|
|
|
@ -1,45 +0,0 @@
|
||||||
--TEST--
|
|
||||||
findMessages() should return composite validation messages flattened
|
|
||||||
--FILE--
|
|
||||||
<?php
|
|
||||||
require 'vendor/autoload.php';
|
|
||||||
|
|
||||||
use Respect\Validation\Exceptions\NestedValidationException;
|
|
||||||
use Respect\Validation\Validator as v;
|
|
||||||
|
|
||||||
$stringMax256 = v::stringType()->length(5, 256);
|
|
||||||
$alnumDot = v::alnum('.');
|
|
||||||
$stringMin8 = v::stringType()->length(8, null);
|
|
||||||
$validator = v::allOf(
|
|
||||||
v::attribute('first_name', $stringMax256)->setName('First Name'),
|
|
||||||
v::attribute('last_name', $stringMax256)->setName('Last Name'),
|
|
||||||
v::attribute('desired_login', $alnumDot)->setName('Desired Login'),
|
|
||||||
v::attribute('password', $stringMin8)->setName('Password'),
|
|
||||||
v::attribute('password_confirmation', $stringMin8)->setName('Password Confirmation'),
|
|
||||||
v::attribute('stay_signedin', v::notEmpty())->setName('Stay signed in'),
|
|
||||||
v::attribute('enable_webhistory', v::notEmpty())->setName('Enabled Web History'),
|
|
||||||
v::attribute('security_question', $stringMax256)->setName('Security Question')
|
|
||||||
)->setName('Validation Form');
|
|
||||||
try {
|
|
||||||
$validator->assert(
|
|
||||||
(object) [
|
|
||||||
'first_name' => 'fiif',
|
|
||||||
'last_name' => null,
|
|
||||||
'desired_login' => null,
|
|
||||||
'password' => null,
|
|
||||||
'password_confirmation' => null,
|
|
||||||
'stay_signedin' => null,
|
|
||||||
'enable_webhistory' => null,
|
|
||||||
'security_question' => null,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
} catch (NestedValidationException $e) {
|
|
||||||
print_r($e->findMessages(['allOf', 'first_name.length']));
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
--EXPECTF--
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[allOf] => All of the required rules must pass for Validation Form
|
|
||||||
[first_name_length] => first_name must have a length between 5 and 256
|
|
||||||
)
|
|
|
@ -1,36 +0,0 @@
|
||||||
--FILE--
|
|
||||||
<?php
|
|
||||||
|
|
||||||
require 'vendor/autoload.php';
|
|
||||||
|
|
||||||
use Respect\Validation\Exceptions\NestedValidationException;
|
|
||||||
use Respect\Validation\Validator as v;
|
|
||||||
|
|
||||||
$object = new \stdClass();
|
|
||||||
$object->email = 'Not an email';
|
|
||||||
$object->password = 'Ale xandre';
|
|
||||||
|
|
||||||
try {
|
|
||||||
v::create()
|
|
||||||
->attribute('email', v::email()->setName('Email Field'))
|
|
||||||
->attribute('password', v::noWhitespace()->setName('Password Field'))
|
|
||||||
->assert($object);
|
|
||||||
} catch (NestedValidationException $exception) {
|
|
||||||
print_r($exception->getMessages());
|
|
||||||
print_r($exception->findMessages([
|
|
||||||
'email' => 'Error: {{name}}',
|
|
||||||
'noWhitespace' => 'Error: {{name}}',
|
|
||||||
]));
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
--EXPECTF--
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[0] => Email Field must be valid email
|
|
||||||
[1] => Password Field must not contain whitespace
|
|
||||||
)
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[email] => Error: Email Field
|
|
||||||
[noWhitespace] => Error: Password Field
|
|
||||||
)
|
|
|
@ -1,57 +0,0 @@
|
||||||
--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
|
|
||||||
)
|
|
|
@ -1,61 +0,0 @@
|
||||||
--FILE--
|
|
||||||
<?php
|
|
||||||
|
|
||||||
require 'vendor/autoload.php';
|
|
||||||
|
|
||||||
use Respect\Validation\Exceptions\NestedValidationException;
|
|
||||||
use Respect\Validation\Validator as v;
|
|
||||||
|
|
||||||
$input = [
|
|
||||||
'contact1' => [
|
|
||||||
'daymark' => 'ffesfewf2232313123212',
|
|
||||||
'building' => [
|
|
||||||
'enable' => 'd',
|
|
||||||
'blank' => 'ffesfewf2232313123212',
|
|
||||||
'powerdown' => '5',
|
|
||||||
'powerup' => 'wdwdwf',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
'contact2' => [
|
|
||||||
'name' => 'wd',
|
|
||||||
'daymark' => 'ffesfewf2232313123212',
|
|
||||||
'building' => [
|
|
||||||
'enable' => '1',
|
|
||||||
'blank' => '1',
|
|
||||||
'powerdown' => '1',
|
|
||||||
'powerup' => '1',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
];
|
|
||||||
|
|
||||||
try {
|
|
||||||
v::create()
|
|
||||||
->each(
|
|
||||||
v::create()
|
|
||||||
->key('name', v::length(1, 50))
|
|
||||||
->key('daymark', v::length(1, 50))
|
|
||||||
->key(
|
|
||||||
'building',
|
|
||||||
v::create()
|
|
||||||
->key('enable', v::length(2, 50))
|
|
||||||
->key('time', v::length(2, 50))
|
|
||||||
->key('powerdown', v::length(2, 50))
|
|
||||||
->key('powerup', v::length(2, 50))
|
|
||||||
)
|
|
||||||
)
|
|
||||||
->assert($input);
|
|
||||||
} catch (NestedValidationException $exception) {
|
|
||||||
print_r(array_filter($exception->findMessages([
|
|
||||||
'each.name' => 'Center name',
|
|
||||||
'each.building.enable' => 'Center time',
|
|
||||||
'each.building.powerdown' => 'Center time',
|
|
||||||
])));
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
--EXPECTF--
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[each_name] => Center name
|
|
||||||
[each_building_enable] => Center time
|
|
||||||
[each_building_powerdown] => Center time
|
|
||||||
)
|
|
|
@ -1,33 +0,0 @@
|
||||||
--FILE--
|
|
||||||
<?php
|
|
||||||
|
|
||||||
require 'vendor/autoload.php';
|
|
||||||
|
|
||||||
use Respect\Validation\Exceptions\NestedValidationException;
|
|
||||||
use Respect\Validation\Validator as v;
|
|
||||||
|
|
||||||
$input = [
|
|
||||||
'user_name' => 'MyName111',
|
|
||||||
'user_surname' => 'MySurname111',
|
|
||||||
'user_tel' => 'asd123',
|
|
||||||
];
|
|
||||||
|
|
||||||
$rules = [
|
|
||||||
v::key('user_name', v::numericVal())->setName('First Name'),
|
|
||||||
v::key('user_surname', v::numericVal())->setName('Second Name'),
|
|
||||||
v::key('user_tel', v::phone())->setName('Phone number'),
|
|
||||||
];
|
|
||||||
|
|
||||||
try {
|
|
||||||
v::allOf($rules)->setName('Validation Form')->assert($input);
|
|
||||||
} catch (NestedValidationException $exception) {
|
|
||||||
print_r($exception->findMessages(array_keys($input)));
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
--EXPECTF--
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[user_name] => user_name must be numeric
|
|
||||||
[user_surname] => user_surname must be numeric
|
|
||||||
[user_tel] => user_tel must be a valid telephone number
|
|
||||||
)
|
|
|
@ -1,23 +0,0 @@
|
||||||
--TEST--
|
|
||||||
Issue #85: findMessages() should not trigger catchable fatal error
|
|
||||||
--FILE--
|
|
||||||
<?php
|
|
||||||
require 'vendor/autoload.php';
|
|
||||||
|
|
||||||
use Respect\Validation\Exceptions\NestedValidationException;
|
|
||||||
use Respect\Validation\Validator;
|
|
||||||
|
|
||||||
$usernameValidator = Validator::alnum('_')->length(1, 15)->noWhitespace();
|
|
||||||
try {
|
|
||||||
$usernameValidator->assert('really messed up screen#name');
|
|
||||||
} catch (NestedValidationException $e) {
|
|
||||||
print_r($e->findMessages(['alnum', 'length', 'noWhitespace']));
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
--EXPECTF--
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[alnum] => "really messed up screen#name" must contain only letters (a-z), digits (0-9) and "_"
|
|
||||||
[length] => "really messed up screen#name" must have a length between 1 and 15
|
|
||||||
[noWhitespace] => "really messed up screen#name" must not contain whitespace
|
|
||||||
)
|
|
|
@ -1,20 +0,0 @@
|
||||||
--FILE--
|
|
||||||
<?php
|
|
||||||
require 'vendor/autoload.php';
|
|
||||||
|
|
||||||
use Respect\Validation\Exceptions\NestedValidationException;
|
|
||||||
use Respect\Validation\Validator as v;
|
|
||||||
|
|
||||||
$usernameValidator = v::alnum()->noWhitespace()->length(1, 15);
|
|
||||||
try {
|
|
||||||
$usernameValidator->assert('really messed up screen#name');
|
|
||||||
} catch (NestedValidationException $exception) {
|
|
||||||
print_r($exception->findMessages(['alnum', 'noWhitespace']));
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
--EXPECTF--
|
|
||||||
Array
|
|
||||||
(
|
|
||||||
[alnum] => "really messed up screen#name" must contain only letters (a-z) and digits (0-9)
|
|
||||||
[noWhitespace] => "really messed up screen#name" must not contain whitespace
|
|
||||||
)
|
|
|
@ -44,26 +44,4 @@ class NestedValidationExceptionTest extends TestCase
|
||||||
self::assertEquals(1, count($composite->getRelated(true)));
|
self::assertEquals(1, count($composite->getRelated(true)));
|
||||||
self::assertContainsOnly($node, $composite->getRelated());
|
self::assertContainsOnly($node, $composite->getRelated());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFindRelatedShouldFindCompositeExceptions(): void
|
|
||||||
{
|
|
||||||
$foo = new AttributeException();
|
|
||||||
$bar = new AttributeException();
|
|
||||||
$baz = new AttributeException();
|
|
||||||
$bat = new AttributeException();
|
|
||||||
$foo->configure('foo');
|
|
||||||
$bar->configure('bar');
|
|
||||||
$baz->configure('baz');
|
|
||||||
$bat->configure('bat');
|
|
||||||
$foo->addRelated($bar);
|
|
||||||
$bar->addRelated($baz);
|
|
||||||
$baz->addRelated($bat);
|
|
||||||
self::assertSame($bar, $foo->findRelated('bar'));
|
|
||||||
self::assertSame($baz, $foo->findRelated('baz'));
|
|
||||||
self::assertSame($baz, $foo->findRelated('bar.baz'));
|
|
||||||
self::assertSame($baz, $foo->findRelated('baz'));
|
|
||||||
self::assertSame($bat, $foo->findRelated('bar.bat'));
|
|
||||||
self::assertNull($foo->findRelated('none'));
|
|
||||||
self::assertNull($foo->findRelated('bar.none'));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue