tôi thêm lớp này vào thư viện/My/Validate/PasswordConfirmation.phpXác nhận mật khẩu trong khuôn khổ zend
<?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;
}
}
?>
sau đó tôi tạo ra hai lĩnh vực trong hình thức của tôi như thế này:
$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)
mọi thứ đều tốt nhưng khi tôi gửi mẫu luôn, tôi nhận được thông báo 'Xác nhận mật khẩu không khớp'? Có gì sai trong mã của tôi
Dưới đây là một cách để làm xác nhận mật khẩu sử dụng xác nhận của Zend (tôi nhận ra rằng nó có thể n ot đã có thể khi câu hỏi này được hỏi): http://stackoverflow.com/questions/347856/zend-form-how-to-check-2-fields-are-identical/3782388#3782388 –