www.pokeroconnor.com


Zend Form Password

May16

When using Zend Form, and you have a password field and confirm password field, it is very handy to use Validation Context. It is described in the Zend documentation, just search for “My_Validate_PasswordConfirmation”. As is typical in Zend docs, the example is shit, they just give a class definition. So, here’s a brief example of I used it.

Basically what you need to do is attach the validator to both the password and the confirm password field. 

So firstly, add the class:

class My_Validate_PasswordConfirmation extends Zend_Validate_Abstract
{
const NOT_MATCH = 'notMatch';

protected $_messageTemplates = array(
self::NOT_MATCH => 'Password confirmation does not match'
);

public function isValid($value, $context = null)
{
$value = (string) $value;
$this->_setValue($value);

if (is_array($context)) {
if (isset($context['password_confirm'])
&& ($value == $context['password_confirm']))
{
return true;
}
} elseif (is_string($context) && ($value == $context)) {
return true;
}

 

 

$this->_error(self::NOT_MATCH);
return false;
}
}

Then, in your form file, use it something like follows:

class RegisterForm extends Zend_Dojo_Form

{

public function init()

{
$form = new Zend_Form();
....
....
....
$custom_pass = new MyValidatePasswordConfirmation();

...
...
$password = $form->createElement('password', 'password')
->addValidator('StringLength', false, array(1,24))
->setLabel('Choose your password:')
->addValidator('Alnum')
->addValidator($custom_pass)
->setRequired(true);

 

 

$password_confirm = $form->createElement('password', 'password_confirm')
->addValidator('StringLength', false, array(1,24))
->setLabel('Confirm your password:')
->addValidator('Alnum')
->addValidator($custom_pass)
->setRequired(true);

It’s fairly straightforward, but of course there’s very few examples I could find, this is probably the best.

//
posted under MySQL, php
One Comment to

“Zend Form Password”

  1. On March 3rd, 2010 at 8:37 am behrang Says:

    hello don’t work for me I think there is no need to add $custom_pass validation in both field but i don’t know how !!! to add it

Email will not be published

Website example

Your Comment: