我這個類添加到庫中/我的/驗證/ 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的驗證做密碼驗證(我知道,它可能n當這個問題被問到時可能):http://stackoverflow.com/questions/347856/zend-form-how-to-check-2-fields-are-identical/3782388#3782388 – 2011-02-14 02:59:36