Created
April 17, 2013 19:38
-
-
Save samsonasik/5407108 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace My\Modules\Authentication\Form; | |
use Zend\Form\Form; | |
use Zend\InputFilter\InputFilterProviderInterface; | |
class ChangePassword extends Form implements InputFilterProviderInterface | |
{ | |
public function __construct() | |
{ | |
parent::__construct('change-password'); | |
} | |
public function init() | |
{ | |
$this->add(array( | |
'type' => 'Zend\Form\Element\Password', | |
'name' => 'old-password', | |
'options' => array( | |
'label' => 'Old Password', | |
), | |
)); | |
$this->add(array( | |
'type' => 'Zend\Form\Element\Password', | |
'name' => 'new-password', | |
'options' => array( | |
'label' => 'New Password', | |
), | |
)); | |
$this->add(array( | |
'type' => 'Zend\Form\Element\Password', | |
'name' => 'new-password-confirm', | |
'options' => array( | |
'label' => 'New Password (confirm)', | |
), | |
)); | |
$this->add(array( | |
'type' => 'Zend\Form\Element\Csrf', | |
'name' => 'csrf', | |
)); | |
$this->add(array( | |
'type' => 'Zend\Form\Element\Button', | |
'name' => 'submit', | |
'options' => array( | |
'label' => 'Change', | |
), | |
'attributes' => array( | |
'type' => 'submit', | |
), | |
)); | |
} | |
public function getInputFilterSpecification() | |
{ | |
return array( | |
'old-password' => array( | |
'filters' => array( | |
array('name' => 'Zend\Filter\StringTrim'), | |
), | |
), | |
'new-password' => array( | |
'filters' => array( | |
array('name' => 'Zend\Filter\StringTrim'), | |
), | |
'validators' => array( | |
array( | |
'name' => 'Zend\Validator\StringLength', | |
'options' => array( | |
'min' => 5, | |
'max' => 63, | |
), | |
), | |
), | |
), | |
'new-password-confirm' => array( | |
'filters' => array( | |
array('name' => 'Zend\Filter\StringTrim'), | |
), | |
'validators' => array( | |
array( | |
'name' => 'My\Validator\Confirm', | |
'options' => array( | |
'field' => 'new-password', | |
), | |
), | |
), | |
), | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace My\Validator; | |
use Zend\Validator\AbstractValidator; | |
class Confirm extends AbstractValidator | |
{ | |
const DIFFERENT_FROM = 'DIFFERENT_FROM'; | |
protected $messageTemplates = array( | |
self::DIFFERENT_FROM => 'Value differs from original one', | |
); | |
private $field; | |
public function __construct(array $options = array()) | |
{ | |
if (! isset($options['field'])) { | |
throw new Exception\InvalidArgumentException('Field to check missing'); | |
} | |
$this->field = $options['field']; | |
parent::__construct($options); | |
} | |
public function isValid($value, $context = null) | |
{ | |
if (! is_array($context) or ! isset($context[$this->field])) { | |
throw new Exception\RuntimeException(sprintf('Field "%s" missing in the context', $this->field)); | |
} | |
$this->setValue($value); | |
if ($value !== $context[$this->field]) { | |
$this->error(self::DIFFERENT_FROM); | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment