Skip to content

Loading…

Fix identical validator #3803

Closed
wants to merge 2 commits into from

2 participants

@iquabius

These changes adds the possibility to use Zend\Validator\Identical to validate a form input that lives inside a fieldset:

See exemple below:

use Zend\Form\Element;
use Zend\Form\Fieldset;
use Zend\Form\Form;
use Zend\InputFilter\Input;
use Zend\InputFilter\InputFilter;

$user = new Fieldset('user'); // (1)
$user->add(array(
    'name' => 'email', // (2)
    'type' => 'Zend\Form\Element\Email',
    'attributes' => array(
        'required' => 'required',
    ),
    'options' => array(
        'label' => 'Email',
    ),
));

$signUp = new Form('signUp');
$signUp->add($user);
$signUp->add(array(
    'name' => 'confirmEmail', // (3)
    'type', 'Zend\Form\Element\Email',
    'options' => array(
        'label' => 'Confirm your email',
    ),
));

$inputFilter = new InputFilter();
$inputFilter->add(array(
    'name' => 'confirmEmail', // references (3)
    'required' => true,
    'validators' => array(
        array(
            'name' => 'Identical',
            'options' => array(
                // 'user' references 'user' fieldset (1), and 'email'
                // references 'email' element inside 'user' fieldset (2)
                'token' => array('user' => 'email'),
            ),
        ),
    ),
));

$signUp->setInputFilter($inputFilter);

$signUp->setData(array(
    'user' => array(
        'email' => 'josias@duarte.com',
    ),
    'confirmEmail' => 'josias@duarte.com',
));

// Will return true because "user['email']" == "confirmEmail"
if ($signUp->isValid()) {
}
@weierophinney weierophinney commented on the diff
library/Zend/Validator/Identical.php
((5 lines not shown))
*/
- public function isValid($value, $context = null)
+ public function isValid($value, array $context = null)
@weierophinney Zend Framework member

This is a backwards incompatible change; test for array inside the method instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney weierophinney added a commit that referenced this pull request
@weierophinney weierophinney [#3803] Remove BC break
- Removed "array" typehint from $context; added check within method body
6f5e977
@weierophinney weierophinney added a commit that referenced this pull request
@weierophinney weierophinney Merge branch 'hotfix/3803' into develop
Forward port #3803
10831d9
@weierophinney weierophinney added a commit that closed this pull request
@weierophinney weierophinney Merge branch 'hotfix/3803'
Close #3803
afd241a
@weierophinney
Zend Framework member

I fixed the BC-breaking issue when merging.

@iquabius

I don't see the diference besides the exeption instead of the native php error, there's some other reason I'm not seeing!?

@ghost Unknown pushed a commit that referenced this pull request
@weierophinney weierophinney [#3803] Remove BC break
- Removed "array" typehint from $context; added check within method body
a9e105e
@ghost Unknown pushed a commit that referenced this pull request
@weierophinney weierophinney Merge branch 'hotfix/3803'
Close #3803
b156f8b
@ghost Unknown pushed a commit that referenced this pull request
@weierophinney weierophinney Merge branch 'hotfix/3803' into develop
Forward port #3803
8f4a7c3
@weierophinney weierophinney added a commit to zendframework/zend-validator that referenced this pull request
@weierophinney weierophinney [zendframework/zf2#3803] Remove BC break
- Removed "array" typehint from $context; added check within method body
614dd81
@weierophinney weierophinney added a commit to zendframework/zend-validator that referenced this pull request
@weierophinney weierophinney Merge branch 'hotfix/3803' 3836aa0
@weierophinney weierophinney added a commit to zendframework/zend-validator that referenced this pull request
@weierophinney weierophinney Merge branch 'hotfix/3803' into develop a6b308c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 108 additions and 6 deletions.
  1. +24 −6 library/Zend/Validator/Identical.php
  2. +84 −0 tests/ZendTest/Validator/IdenticalTest.php
View
30 library/Zend/Validator/Identical.php
@@ -87,7 +87,7 @@ public function getToken()
*/
public function setToken($token)
{
- $this->tokenString = (is_array($token) ? implode($token) : (string) $token);
+ $this->tokenString = (is_array($token) ? var_export($token, true) : (string) $token);
$this->token = $token;
return $this;
}
@@ -121,15 +121,33 @@ public function setStrict($strict)
* @param mixed $value
* @param array $context
* @return bool
+ * @throws Exception\RuntimeException if the token doesn't exist in the context array
*/
- public function isValid($value, $context = null)
+ public function isValid($value, array $context = null)
@weierophinney Zend Framework member

This is a backwards incompatible change; test for array inside the method instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
{
$this->setValue($value);
- if (($context !== null) && isset($context) && array_key_exists($this->getToken(), $context)) {
- $token = $context[$this->getToken()];
- } else {
- $token = $this->getToken();
+ $token = $this->getToken();
+
+ if ($context !== null) {
+ if (is_array($token)) {
+ while (is_array($token)){
+ $key = key($token);
+ if (!isset($context[$key])) {
+ break;
+ }
+ $context = $context[$key];
+ $token = $token[$key];
+ }
+ }
+
+ // if $token is an array it means the above loop didn't went all the way down to the leaf,
+ // so the $token structure doesn't match the $context structure
+ if (is_array($token) || !isset($context[$token])) {
+ throw new Exception\RuntimeException("The token doesn't exist in the context");
+ } else {
+ $token = $context[$token];
+ }
}
if ($token === null) {
View
84 tests/ZendTest/Validator/IdenticalTest.php
@@ -129,4 +129,88 @@ public function testEqualsMessageVariables()
$this->assertAttributeEquals($validator->getOption('messageVariables'),
'messageVariables', $validator);
}
+
+ public function testValidatingStringTokenInContext()
+ {
+ $this->validator->setToken('email');
+
+ $this->assertTrue($this->validator->isValid(
+ 'john@doe.com',
+ array('email' => 'john@doe.com')
+ ));
+
+ $this->assertFalse($this->validator->isValid(
+ 'john@doe.com',
+ array('email' => 'harry@hoe.com')
+ ));
+
+ $this->assertFalse($this->validator->isValid(
+ 'harry@hoe.com',
+ array('email' => 'john@doe.com')
+ ));
+ }
+
+ public function testValidatingArrayTokenInContext()
+ {
+ $this->validator->setToken(array('user' => 'email'));
+
+ $this->assertTrue($this->validator->isValid(
+ 'john@doe.com',
+ array(
+ 'user' => array(
+ 'email' => 'john@doe.com'
+ )
+ )
+ ));
+
+ $this->assertFalse($this->validator->isValid(
+ 'john@doe.com',
+ array(
+ 'user' => array(
+ 'email' => 'harry@hoe.com'
+ )
+ )
+ ));
+
+ $this->assertFalse($this->validator->isValid(
+ 'harry@hoe.com',
+ array(
+ 'user' => array(
+ 'email' => 'john@doe.com'
+ )
+ )
+ ));
+ }
+
+ public function testSetStringTokenNonExistentInContext()
+ {
+ $this->validator->setToken('email');
+ $this->setExpectedException(
+ 'Zend\Validator\Exception\RuntimeException',
+ "The token doesn't exist in the context"
+ );
+
+ $this->validator->isValid(
+ 'john@doe.com',
+ array('name' => 'john') // There's no 'email' key here, must throw an exception
+ );
+ }
+
+ public function testSetArrayTokenNonExistentInContext()
+ {
+ $this->validator->setToken(array('user' => 'email'));
+ $this->setExpectedException(
+ 'Zend\Validator\Exception\RuntimeException',
+ "The token doesn't exist in the context"
+ );
+
+ $this->validator->isValid(
+ 'john@doe.com',
+ array(
+ 'admin' => array( // Here is 'admin' instead of 'user', must throw an exception
+ 'email' => 'john@doe.com'
+ )
+ )
+ );
+ }
}
Something went wrong with that request. Please try again.