English 中文(简体)
密码确认在Zend框架中
原标题:Password Confirmation in zend framework

我把这个类添加到library/My/Validate/PasswordConfirmation.php。

<?php 
require_once  Zend/Validate/Abstract.php ;
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 ])
                && ($value == $context[ password ]))
            {
                return true;
            }
        } elseif (is_string($context) && ($value == $context)) {
            return true;
        }

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

然后我在我的表单中创建了两个字段,就像这样:

       $userPassword = $this->createElement( password ,  user_password );
    $userPassword->setLabel( Password:  );
    $userPassword->setRequired( true );
    $this->addElement($userPassword);

    //create the form elements user_password repeat
    $userPasswordRepeat = $this->createElement( password ,  password_confirm );
    $userPasswordRepeat->setLabel( Password repeat:  );
    $userPasswordRepeat->setRequired( true );
    $userPasswordRepeat->addPrefixPath( My_Validate , My/Validate , validate );

    $userPasswordRepeat->addValidator( PasswordConfirmation );
    $this->addElement($userPasswordRepeat)

一切都好,但每当我提交表单时,我总是收到“密码确认不匹配”的消息?我的代码有什么问题?

最佳回答

我认为您可能想要使用$context[user_password],因为那是您的“第一个”密码元素的名称。

问题回答

你不需要重写Zend_Form->isValid方法或使用超全局变量$_POST,可以检查一下这个:

$frmPassword1=new Zend_Form_Element_Password( password );
$frmPassword1->setLabel( Password )
    ->setRequired( true )
    ->addFilter(new Zend_Filter_StringTrim())
    ->addValidator(new Zend_Validate_NotEmpty());

$frmPassword2=new Zend_Form_Element_Password( confirm_password );
$frmPassword2->setLabel( Confirm password )
    ->setRequired( true )
    ->addFilter(new Zend_Filter_StringTrim())
    ->addValidator(new Zend_Validate_Identical( password ));

更简便的方法:

    $password = new Zend_Form_Element_Password( password );
    $password->setLabel( Password: )
            ->addValidator( StringLength , false, array(6,24))
            ->setLabel( Choose your password: )
            ->setRequired(true);

    $password2 = new Zend_Form_Element_Password( password-confirm );
    $password2->setLabel( Confirm: )
            ->addValidator( StringLength , false, array(6,24))
            ->setLabel( Confirm your password: )
            ->addValidator(new Zend_Validate_Identical($_POST[ password ]))
            ->setRequired(true);

有一种更好的方法可以做到这一点。在您的表单中,在确认密码字段上放置相同的验证器,然后只需覆盖$form->isValid()方法以设置要验证的值:

public function __construct($options = NULL)
{
   // ...
   $confirm->addValidator( Identical );
   // ...
}
public function isValid($data)
{
    $confirm = $this->getElement( confirm_password );
    $confirm->getValidator( Identical )->setToken($data[ password ]);
    return parent::isValid($data);
}

使验证器可重用。不要在验证器中硬编码字段名。看看这个更通用的IdenticalField Validator





相关问题
Encrypting SALTEDHASHED weblogic password in java

How to encrypt SALTEDHASHED password (used by Weblogic) in java? I need to be able to hash passwords in exactly the same way as WebLogic s authenticator does. Preferably without using WebLogic s ...

Should I support Unicode in passwords?

I would like to allow my users to use Unicode for their passwords. However I see a lot of sites don t support that (e.g. Gmail, Hotmail). So I m wondering if there s some technical or usability ...

Why does my method return a null password?

I have two classes: a Generator class and a SystemManagement class. The Generator class, I can generate a password. The SystemManagement class imports Generator (from another package) and includes ...

How to mask password in c?

In C, I want to display every single character that the user type as * (Ex, Please type in your password: *****) I m searching around but can t be able to find a solution for this. I m working on ...

MySQL password function

Is it considered good or bad practice to use MySQL s password function to hash passwords used by an application? I can see pros and cons. I m curious if there is a general consensus on whether it is ...

How should I store a user s LDAP password in a cookie?

So I have this black box authentication method, handed down to me from the accounts people, which basically amounts to ldap_bind($connection, $username, $password). But of course, I want my users to ...

Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Password protect a simple web site

I ve got an HTML-only website (with a bit of inconsequential javascript). The static pages are generated from a photo-blogging program that I created. There are hundreds of pages and thousands of ...

热门标签