0

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

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

$password2 是一个问题

由于这个问题,也不确定是否相同的验证器正在工作。

Zend 框架 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
    }

}
4

1 回答 1

1

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

$validatorChain2->addValidator(new Zend_Validate_Identical(array('token' => 'password1', 'messages' => ....)), true);
于 2012-07-11T11:22:48.677 回答