Zend Framework验证器链消息

时间:2012-07-11 11:15:07

标签: validation zend-framework

努力理解为什么我的验证器链显示我输入的值的错误消息。

目前,当我输入不相同的密码而不仅仅是预期的第3条消息时,我的模型代码会显示不正确的消息('请重新输入8-25个字母数字字符之间的密码并匹配第一个密码'当期待'重新输入的密码必须先匹配。')当我输入长度为8 -25个字符的不匹配密码时。

$ password2是给出问题的人

还不确定由于此问题而导致的缩进验证器是否正常工作。

Zend Framework版本1.11

与上述相关的特定代码块

$password2 = new Zend_Form_Element_Password('password2');
$password2->setAttrib('size', 25);
$password2->setRequired(true);
$validatorChain2 = new Zend_Validate();
$validatorChain2->addValidator(new Zend_Validate_StringLength(array('min' => 8,'max' => 25)), true);
$validatorChain2->addValidator(new Zend_Validate_Alnum(false), true);
$validatorChain2->addValidator(new Zend_Validate_Identical('password1'), true);
$password2->addValidator($validatorChain2);
$password2->addErrorMessage('Please retype password between 8-25 alphanumeric characters and matching the first password');
$password2->addErrorMessage('Passwords can only be alphanumeric characters.');
        $password2->addErrorMessage('Retyped password must match first.');

完整型号代码

class Application_Model_RegisterNewAccount extends Zend_Form
{

    public function __construct($options = null)
    {
        // TODO: The errors are been grouped together at the top of the page now but the original error messages below each field need to be removed
        parent::__construct($options);

        $this->setName('newaccountregistration');
        $this->setMethod('post');
        $this->setAction($options['action']);   // Variable action passed via parameter array for argument 'action'

        $email = new Zend_Form_Element_Text('email');   // Create form text field
        $email->setAttrib('size', 75);  // Set for, element max size/length
        $email->setRequired(true);  // Make this field a required item

        $validator_email = new Zend_Validate_EmailAddress();
        $email->addValidator($validator_email);   // Set validator type and minimum and maximum size required
        $email->addErrorMessage('Please provide a valid email address');

        $firstname = new Zend_Form_Element_Text('firstname');
        $firstname->setAttrib('size', 35);
        $firstname->setRequired(true);

        $validator_firstname = new Zend_Validate_StringLength(array(1,35));
        $firstname->addValidator($validator_firstname);
        $firstname->addErrorMessage('Please provide your first name between 1-35 characters');

        $surname = new Zend_Form_Element_Text('surname');
        $surname->setAttrib('size', 35);
        $surname->setRequired(true);

        $validator_surname = new Zend_Validate_StringLength(array(1,35));
        $surname->addValidator($validator_surname);
        $surname->addErrorMessage('Please provide your first name between 1-35 characters');

        $password1 = new Zend_Form_Element_Password('password1');
        $password1->setAttrib('size', 25);
        $password1->setRequired(true);
        $validatorChain1 = new Zend_Validate();
        $validatorChain1->addValidator(new Zend_Validate_StringLength(array('min' => 8,'max' => 25)), true);
        $validatorChain1->addValidator(new Zend_Validate_Alnum(false), true);
        $password1->addValidator($validatorChain1);
        $password1->addErrorMessage('Please provide a password between 8-25 alphanumeric characters');
        $password1->addErrorMessage('Passwords can only be alphanumeric characters.');

        $password2 = new Zend_Form_Element_Password('password2');

        $password2->setAttrib('size', 25);
        $password2->setRequired(true);
        $validatorChain2 = new Zend_Validate();
        $validatorChain2->addValidator(new Zend_Validate_StringLength(array('min' => 8,'max' => 25)), true);
        $validatorChain2->addValidator(new Zend_Validate_Alnum(false), true);
        $validatorChain2->addValidator(new Zend_Validate_Identical('password1'), true);
        $password2->addValidator($validatorChain2);
        $password2->addErrorMessage('Please retype password between 8-25 alphanumeric characters and matching the first password');
        $password2->addErrorMessage('Passwords can only be alphanumeric characters.');
        $password2->addErrorMessage('Retyped password must match first.');

        $submit = new Zend_Form_Element_Submit('submit');   // Added submit button
        $submit->setLabel('Register');
        $submit->removeDecorator('DtDdWrapper'); // Remove the default DD div wrapper

        $this->setDecorators( array( array('ViewScript',
            array('viewScript' => '_register.phtml'))));    // The form html page

        $this->addElements(array($email, $firstname, $surname, $password1, $password2, $submit));   // Add all the form elements
    }

}

1 个答案:

答案 0 :(得分:1)

你应该像这样实例化它......

$validatorChain2->addValidator(new Zend_Validate_Identical(array('token' => 'password1', 'messages' => ....)), true);