From 69f433db6b01ab1b93741a87fa0e98557dbabd14 Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Thu, 10 Sep 2015 19:10:18 -0300 Subject: [PATCH] Create method `getMessages()` on nested exception --- docs/README.md | 31 +++++++++++++++- .../Exceptions/AbstractNestedException.php | 15 ++++++++ .../NestedValidationExceptionInterface.php | 1 + ...de_all_validation_messages_in_a_chain.phpt | 37 +++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 tests/integration/get_messages_should_include_all_validation_messages_in_a_chain.phpt diff --git a/docs/README.md b/docs/README.md index d7ec9660..db74affc 100644 --- a/docs/README.md +++ b/docs/README.md @@ -121,7 +121,7 @@ The printed message is exactly this, as a text tree: \-"really messed up screen#name" must have a length between 1 and 15 ``` -## Getting Messages +## Requesting Messages The text tree is fine, but unusable on a HTML form or something more custom. You can use `findMessages()` for that: @@ -138,6 +138,35 @@ try { `findMessages()` returns an array with messages from the requested validators. +## Getting Messages + +Sometimes you just need all the messages, for that you can use `getMessages()`. +It will return all messages from the rules that did not pass the validation. + +```php +try { + Validator::key('username', Validator::length(2, 32)) + ->key('birthdate', Validator::date()) + ->key('password', Validator::notEmpty()) + ->key('email', Validator::email()) + ->assert($input); +} catch (NestedValidationExceptionInterface $e) { + print_r($e->getMessages()); +} +``` + +The code above may display something like: + +``` +Array +( + [0] => username must have a length between 2 and 32 + [1] => birthdate must be a valid date + [2] => password must not be empty + [3] => Key email must be present +) +``` + ## Custom Messages Getting messages as an array is fine, but sometimes you need to customize them in order diff --git a/library/Exceptions/AbstractNestedException.php b/library/Exceptions/AbstractNestedException.php index 56057da3..2a97185c 100644 --- a/library/Exceptions/AbstractNestedException.php +++ b/library/Exceptions/AbstractNestedException.php @@ -73,6 +73,21 @@ class AbstractNestedException extends ValidationException implements NestedValid } } + public function getMessages() + { + $messages = array(); + foreach ($this->getIterator() as $key => $exception) { + if ($exception instanceof AbstractNestedException + && count($exception->getRelated()) > 0) { + continue; + } + + $messages[] = $exception->getMessage(); + } + + return $messages; + } + public function getFullMessage() { $message = array(); diff --git a/library/Exceptions/NestedValidationExceptionInterface.php b/library/Exceptions/NestedValidationExceptionInterface.php index 85f43016..e98509d0 100644 --- a/library/Exceptions/NestedValidationExceptionInterface.php +++ b/library/Exceptions/NestedValidationExceptionInterface.php @@ -14,5 +14,6 @@ namespace Respect\Validation\Exceptions; interface NestedValidationExceptionInterface extends ValidationExceptionInterface { public function findMessages(array $paths); + public function getMessages(); public function getFullMessage(); } diff --git a/tests/integration/get_messages_should_include_all_validation_messages_in_a_chain.phpt b/tests/integration/get_messages_should_include_all_validation_messages_in_a_chain.phpt new file mode 100644 index 00000000..d78269e1 --- /dev/null +++ b/tests/integration/get_messages_should_include_all_validation_messages_in_a_chain.phpt @@ -0,0 +1,37 @@ +--TEST-- +getMessages() should include all validation messages in a chain +--FILE-- + 'u', + 'birthdate' => 'Not a date', + 'password' => '', + ); + + Validator::key('username', Validator::length(2, 32)) + ->key('birthdate', Validator::date()) + ->key('password', Validator::notEmpty()) + ->key('email', Validator::email()) + ->assert($input); +} catch (NestedValidationExceptionInterface $e) { + print_r($e->getMessages()); +} +?> +--EXPECTF-- +Array +( + [0] => username must have a length between 2 and 32 + [1] => birthdate must be a valid date + [2] => password must not be empty + [3] => Key email must be present +)