gpt4 book ai didi

zend-framework - Zend框架中的密码确认

转载 作者:行者123 更新时间:2023-12-03 22:42:57 24 4
gpt4 key购买 nike

我将这个类添加到 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)

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

最佳答案

您不需要覆盖 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'));

关于zend-framework - Zend框架中的密码确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2389954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com