I use a custom validator for this functionality. Add it to the validation chain in the form model:

        $confirmPassword = new Zend_Form_Element_Password("password_confirm");
        $confirmPassword->setLabel('Confirm Password')
                ->setDescription('Please re-enter your password.')
                ->addValidator('PasswordConfirm')
                ->addValidator('StringLength', false, array(5, 14))
                ->setAttribs(array('size'=>30))
                ->setRequired(true);
        $this->addElement($confirmPassword);

The custom validator goes in your apps lib. /library/MF/Validate/ PasswordConfirm.php.

This is some code i got off this list or on the Zend website that I use. It assumes the password confirm field is called "password." I'm sure a generic one could be made for all confirms.

<?php

require_once 'Zend/Validate/Abstract.php';

class MF_Validate_PasswordConfirm extends Zend_Validate_Abstract
{
        const NOT_MATCH = 'notMatch';
        
protected $_messageTemplates = array(self::NOT_MATCH => 'Passwords do not match.');
        
        public function isValid($value, $context = null)
        {
                $value = (string) $value;
                $this->_setValue($value);
                
                if (is_array($context)) {
if (isset($context['password']) && ($value == $context['password'])) {
                                return true;
                        }
                } elseif (is_string($context) && ($value == $context)) {
                        return true;
                }
                
                $this->_error(self::NOT_MATCH);
                return false;
        }
}

Haven't tried the Dojo password box yet but looks cool.

EH


On Jan 5, 2009, at 12:14 PM, webPragmatist wrote:

Is it possible to pass the POST variable(s) once instead of twice when using the identical validator?

What I have is a method in a model that checks a param which is passed when the form is generated in the controller. Then the controller (if the request is post) runs isValid($_POST).

This works fine I just wish there was a way to access the post variable from the same $_POST array that is sent using isValid instead of passing variables twice to the form object (model).

    public function getAccountCreateForm($postPass = null)
    {
        // Add a password form element
$password = new Zend_Dojo_Form_Element_PasswordTextBox('password');
        $password->setLabel('Password:')
                 ->setRequired(true)
                 ->setTrim(true)
->setInvalidMessage('Please provide a valid password');
        $form->addElement($password);

        // Add a confirm password form element
        $confirmPassword = new Zend_Dojo_Form_Element_PasswordTextBox(
'confirmPassword');
        $confirmPassword->setLabel('Confirm Password:')
                 ->setRequired(true)
                 ->setTrim(true)
->setInvalidMessage('Please provide a valid password')
                 ->addValidator('identical',
                    true,
                    array($postPass));
        $form->addElement($confirmPassword);
    }

View this message in context: Zend_Form / Identical Validator and comparing form fields
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to