From 64ec329663af8d26693e8ca68695bc03a8039594 Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Wed, 23 May 2018 14:03:47 +0200 Subject: [PATCH] Remove findMessages() from NestedValidationException --- docs/README.md | 41 -------- .../Exceptions/NestedValidationException.php | 98 ------------------- ...apply_templates_to_flattened_messages.phpt | 51 ---------- ...mposite_validation_messages_flattened.phpt | 45 --------- tests/integration/issue-620.phpt | 36 ------- tests/integration/issue-689.phpt | 57 ----------- tests/integration/issue-690.phpt | 61 ------------ tests/integration/issue-719.phpt | 33 ------- ...uld_not_trigger_catchable_fatal_error.phpt | 23 ----- tests/integration/readme/example_3.phpt | 20 ---- .../NestedValidationExceptionTest.php | 22 ----- 11 files changed, 487 deletions(-) delete mode 100644 tests/integration/find_messages_should_apply_templates_to_flattened_messages.phpt delete mode 100644 tests/integration/find_messages_should_return_composite_validation_messages_flattened.phpt delete mode 100644 tests/integration/issue-620.phpt delete mode 100644 tests/integration/issue-689.phpt delete mode 100644 tests/integration/issue-690.phpt delete mode 100644 tests/integration/issue-719.phpt delete mode 100644 tests/integration/issue_85_find_messages_should_not_trigger_catchable_fatal_error.phpt delete mode 100644 tests/integration/readme/example_3.phpt diff --git a/docs/README.md b/docs/README.md index 4d6ec275..1243c15c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -141,7 +141,6 @@ $usernameValidator->validate('#$%'); //false - Extends the `Respect\Validation\Exceptions\ValidationException` class - Usually thrown when the `assert()` fails - Available methods: - - `findMessages()`; - `getFullMessage()`; - `getMessages()`; - 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 You're also able to translate your message to another language with Validation. diff --git a/library/Exceptions/NestedValidationException.php b/library/Exceptions/NestedValidationException.php index cea335e5..58f1a847 100644 --- a/library/Exceptions/NestedValidationException.php +++ b/library/Exceptions/NestedValidationException.php @@ -36,80 +36,6 @@ class NestedValidationException extends ValidationException implements IteratorA 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 */ @@ -263,30 +189,6 @@ class NestedValidationException extends ValidationException implements IteratorA 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 * diff --git a/tests/integration/find_messages_should_apply_templates_to_flattened_messages.phpt b/tests/integration/find_messages_should_apply_templates_to_flattened_messages.phpt deleted file mode 100644 index cc90544a..00000000 --- a/tests/integration/find_messages_should_apply_templates_to_flattened_messages.phpt +++ /dev/null @@ -1,51 +0,0 @@ ---TEST-- -findMessages() should apply templates to flattened messages ---FILE-- -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" -) diff --git a/tests/integration/find_messages_should_return_composite_validation_messages_flattened.phpt b/tests/integration/find_messages_should_return_composite_validation_messages_flattened.phpt deleted file mode 100644 index c22392fd..00000000 --- a/tests/integration/find_messages_should_return_composite_validation_messages_flattened.phpt +++ /dev/null @@ -1,45 +0,0 @@ ---TEST-- -findMessages() should return composite validation messages flattened ---FILE-- -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 -) diff --git a/tests/integration/issue-620.phpt b/tests/integration/issue-620.phpt deleted file mode 100644 index 4f6fb4b1..00000000 --- a/tests/integration/issue-620.phpt +++ /dev/null @@ -1,36 +0,0 @@ ---FILE-- -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 -) diff --git a/tests/integration/issue-689.phpt b/tests/integration/issue-689.phpt deleted file mode 100644 index 0c8032bc..00000000 --- a/tests/integration/issue-689.phpt +++ /dev/null @@ -1,57 +0,0 @@ ---FILE-- - [ - '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 -) diff --git a/tests/integration/issue-690.phpt b/tests/integration/issue-690.phpt deleted file mode 100644 index 06576308..00000000 --- a/tests/integration/issue-690.phpt +++ /dev/null @@ -1,61 +0,0 @@ ---FILE-- - [ - '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 -) diff --git a/tests/integration/issue-719.phpt b/tests/integration/issue-719.phpt deleted file mode 100644 index c9a71fa0..00000000 --- a/tests/integration/issue-719.phpt +++ /dev/null @@ -1,33 +0,0 @@ ---FILE-- - '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 -) diff --git a/tests/integration/issue_85_find_messages_should_not_trigger_catchable_fatal_error.phpt b/tests/integration/issue_85_find_messages_should_not_trigger_catchable_fatal_error.phpt deleted file mode 100644 index 9f05310f..00000000 --- a/tests/integration/issue_85_find_messages_should_not_trigger_catchable_fatal_error.phpt +++ /dev/null @@ -1,23 +0,0 @@ ---TEST-- -Issue #85: findMessages() should not trigger catchable fatal error ---FILE-- -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 -) diff --git a/tests/integration/readme/example_3.phpt b/tests/integration/readme/example_3.phpt deleted file mode 100644 index e8ff71ce..00000000 --- a/tests/integration/readme/example_3.phpt +++ /dev/null @@ -1,20 +0,0 @@ ---FILE-- -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 -) diff --git a/tests/unit/Exceptions/NestedValidationExceptionTest.php b/tests/unit/Exceptions/NestedValidationExceptionTest.php index 3c14fa7b..891a4dad 100644 --- a/tests/unit/Exceptions/NestedValidationExceptionTest.php +++ b/tests/unit/Exceptions/NestedValidationExceptionTest.php @@ -44,26 +44,4 @@ class NestedValidationExceptionTest extends TestCase self::assertEquals(1, count($composite->getRelated(true))); 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')); - } }