diff --git a/README.md b/README.md index 0737c285..c52ab847 100644 --- a/README.md +++ b/README.md @@ -170,7 +170,7 @@ Then, the following validation code: try { $validBlogPost->assert($blogPost); } catch (\InvalidArgumentException $e) { - echo $e->findRelated('author', 'username', 'noWhitespace')->getMainMessage(); + echo $e->findRelated('author.username.noWhitespace')->getMainMessage(); } Finds the specific noWhitespace message inside author->username and prints it: @@ -185,6 +185,16 @@ You can export the messages as a plain array to use in template engines for exam $messages = $e->findMessages('author.username.noWhitespace', 'title'); } +You can also set runtime templates for those found messages: + + try { + $validBlogPost->assert($blogPost); + } catch (\InvalidArgumentException $e) { + $messages = $e->findMessages( + 'author.username.noWhitespace' => 'Author username must not have any whitespace', + 'title' => 'Article title is invalid.' + ); + } Using Zend and/or Symfony validators ------------------------------------ diff --git a/library/Respect/Validation/Exceptions/AbstractNestedException.php b/library/Respect/Validation/Exceptions/AbstractNestedException.php index 8d94c3b7..e836547c 100644 --- a/library/Respect/Validation/Exceptions/AbstractNestedException.php +++ b/library/Respect/Validation/Exceptions/AbstractNestedException.php @@ -19,24 +19,29 @@ class AbstractNestedException extends ValidationException return $this; } - public function findMessages() + public function findMessages(array $paths) { $messages = array(); - foreach (func_get_args() as $finder) { - $e = call_user_func_array( - array($this, 'findRelated'), explode('.', $finder) - ); - $finder = str_replace('.', '_', $finder); - $messages[$finder] = $e ? $e->getMainMessage() : ''; + foreach ($paths as $key => $value) { + $numericKey = is_numeric($key); + $path = $numericKey ? $value : $key; + + $e = $this->findRelated($path); + + if (!$numericKey) + $e->setTemplate($value); + + $path = str_replace('.', '_', $path); + $messages[$path] = $e ? $e->getMainMessage() : ''; } return $messages; } - public function findRelated() + public function findRelated($path) { $target = $this; - $path = func_get_args(); + $path = explode('.', $path); while (!empty($path) && $target !== false) $target = $target->getRelatedByName(array_shift($path)); diff --git a/library/Respect/Validation/package.xml b/library/Respect/Validation/package.xml index 05098262..2cbc6a8b 100644 --- a/library/Respect/Validation/package.xml +++ b/library/Respect/Validation/package.xml @@ -10,8 +10,8 @@ alexandre@gaigalas.net yes - 2011-04-27 - + 2011-04-29 + 0.2 0.2 @@ -27,7 +27,7 @@ - + @@ -97,7 +97,7 @@ - + @@ -143,7 +143,7 @@ alpha alpha - 2011-04-27 + 2011-04-29 BSD Style . diff --git a/tests/library/Respect/Validation/Exceptions/AbstractNestedExceptionTest.php b/tests/library/Respect/Validation/Exceptions/AbstractNestedExceptionTest.php index cdcdef37..6dcbd206 100644 --- a/tests/library/Respect/Validation/Exceptions/AbstractNestedExceptionTest.php +++ b/tests/library/Respect/Validation/Exceptions/AbstractNestedExceptionTest.php @@ -40,14 +40,14 @@ class AbstractNestedExceptionTest extends \PHPUnit_Framework_TestCase $baz->addRelated($bat); $this->assertSame($bar, $foo->findRelated('bar')); $this->assertSame($baz, $foo->findRelated('baz')); - $this->assertSame($baz, $foo->findRelated('bar', 'baz')); + $this->assertSame($baz, $foo->findRelated('bar.baz')); $this->assertSame($baz, $foo->findRelated('baz')); - $this->assertSame($bat, $foo->findRelated('bar', 'bat')); + $this->assertSame($bat, $foo->findRelated('bar.bat')); $this->assertSame(false, $foo->findRelated('none')); - $this->assertSame(false, $foo->findRelated('bar', 'none')); + $this->assertSame(false, $foo->findRelated('bar.none')); } - public function testAsArray() + public function testFindMessages() { $stringMax256 = v::string()->length(5, 256); $alnumDot = v::alnum('.'); @@ -77,11 +77,54 @@ class AbstractNestedExceptionTest extends \PHPUnit_Framework_TestCase ); } catch (ValidationException $e) { $messages = $e->findMessages( - 'allOf', - 'first_name.length' + array('allOf', 'first_name.length') ); - $this->assertEquals($messages['allOf'], 'These 8 rules must pass for Validation Form'); - $this->assertEquals($messages['first_name_length'], '"fiif" must have a length between 5 and 256'); + $this->assertEquals($messages['allOf'], + 'These 8 rules must pass for Validation Form'); + $this->assertEquals($messages['first_name_length'], + '"fiif" must have a length between 5 and 256'); + } + } + + public function testFindMessagesTemplates() + { + $stringMax256 = v::string()->length(5, 256); + $alnumDot = v::alnum('.'); + $stringMin8 = v::string()->length(8, null); + $v = 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 { + $v->assert( + (object) array( + 'first_name' => 'fiif', + 'last_name' => null, + 'desired_login' => null, + 'password' => null, + 'password_confirmation' => null, + 'stay_signedin' => null, + 'enable_webhistory' => null, + 'security_question' => null, + ) + ); + } catch (ValidationException $e) { + $messages = $e->findMessages( + array( + 'allOf' => 'Invalid {{name}}', + 'first_name.length' => 'Invalid length for {{name}}' + ) + ); + $this->assertEquals($messages['allOf'], + 'Invalid Validation Form'); + $this->assertEquals($messages['first_name_length'], + 'Invalid length for "fiif"'); } }