mirror of
https://github.com/Respect/Validation.git
synced 2024-06-22 07:16:32 +02:00
New API for findRelated() and findMessages() on AbstractNestedException
This commit is contained in:
parent
1ae18f2f5a
commit
e635db4a19
12
README.md
12
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
|
||||
------------------------------------
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -10,8 +10,8 @@
|
|||
<email>alexandre@gaigalas.net</email>
|
||||
<active>yes</active>
|
||||
</lead>
|
||||
<date>2011-04-27</date>
|
||||
<time>20:40:59</time>
|
||||
<date>2011-04-29</date>
|
||||
<time>16:00:26</time>
|
||||
<version>
|
||||
<release>0.2</release>
|
||||
<api>0.2</api>
|
||||
|
@ -27,7 +27,7 @@
|
|||
<contents>
|
||||
<dir baseinstalldir="Respect/Validation" name="/">
|
||||
<file baseinstalldir="Respect/Validation" md5sum="1f0a6acb3e14603fa75a6dc96052947e" name="Exceptions/AbstractGroupedException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="d1bf3c5bd8867251770188f303433742" name="Exceptions/AbstractNestedException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="254187f37942c9bdba5315659e5ee0c1" name="Exceptions/AbstractNestedException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="8a4dd9c923407917701a243e5b8cbe98" name="Exceptions/AllOfException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="d895b3896ffa43b932c790662c7502c0" name="Exceptions/AlnumException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="d86f455852a42aba71393c2f643481b3" name="Exceptions/AlphaException.php" role="php" />
|
||||
|
@ -97,7 +97,7 @@
|
|||
<file baseinstalldir="Respect/Validation" md5sum="c77fa0fa5313cfd858330ac920955da2" name="Rules/Instance.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="4c8ea7aa820f838eeb9ceb4ab844f54e" name="Rules/Int.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="bb206fe3c0f6bad27b750fc7757eec83" name="Rules/Ip.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="550026d0c64f0a4f85cfe2ae5f452073" name="Rules/Key.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="3a4b37844977b01e20d271ee5a2b0182" name="Rules/Key.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="f67d417f8067c8e547ae0486dc67d7e4" name="Rules/Length.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="998ade345898447919a43730c1334282" name="Rules/Max.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="492fd7483a84888f37de24f25ade58a3" name="Rules/Min.php" role="php" />
|
||||
|
@ -143,7 +143,7 @@
|
|||
<release>alpha</release>
|
||||
<api>alpha</api>
|
||||
</stability>
|
||||
<date>2011-04-27</date>
|
||||
<date>2011-04-29</date>
|
||||
<license uri="http://www.opensource.org/licenses/bsd-license.php">BSD Style</license>
|
||||
<notes>
|
||||
.
|
||||
|
|
|
@ -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"');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue