2009-10-27 99 views
12

在我的表單中,我試圖驗證用戶兩次填充相同的值(以確保他們沒有犯錯)。我認爲這就是Zend_Validate_Identical的作用,但我不太清楚如何使用它。這是我到目前爲止:如何驗證zend形式的密碼字段?

$this->addElement('password', 'password', array(
     'label'  => 'Password:', 
     'required' => true, 
     'validators' => array(
      'Identical' => array(What do I put here?) 
     ) 
    )); 
$this->addElement('password', 'verifypassword', array(
     'label'  => 'Verify Password:', 
     'required' => true, 
     'validators' => array(
      'Identical' => array(What do I put here?) 
     ) 
    )); 

我是否需要這兩個元素?我在數組中放入了什麼?

+0

使用Zend_Validate_Identical和另一個元素作爲'token'的最新答案:http://stackoverflow.com/questions/1628606/how-to-verify-password-field-in-zend-form/3653416#3653416 – 2010-09-23 21:07:36

回答

27

對於它的價值,支持比較模型中的兩個相同的表單域被添加到1.10.5版本。我寫了一篇關於此問題的簡短教程,您可以通過下面的鏈接訪問該教程,但底線是Zend_Validate_Identical驗證程序已被重構爲接受表單字段名稱作爲輸入。例如,爲了表單字段PSWD和confirm_pswd的值進行比較,你會附加驗證器confirm_pswd像這樣:

$confirmPswd->addValidator('Identical', false, array('token' => 'pswd')); 

就像一個魅力。

有關更完整的示例,請參閱Validating Identical Passwords with the Zend Framework

+0

+1這個答案爲我節省了很多時間... – 2012-04-12 18:35:58

+0

我怎樣才能使用這個工具進行添加和編輯操作@ Jason,@ saji89 – ehp 2012-09-21 14:25:05

+0

您節省了我的一天。非常感謝 ! – 2012-11-15 12:46:11

1
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_confirm']) 
      && ($value == $context['password_confirm'])) 
     { 
      return true; 
     } 
    } elseif (is_string($context) && ($value == $context)) { 
     return true; 
    } 

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

http://framework.zend.com/manual/en/zend.form.elements.html

4

我不能在此刻測試,但我認爲這可能工作:

$this->addElement('password', 'password', array(
    'label'  => 'Password:', 
    'required' => true 
)); 
$this->addElement('password', 'verifypassword', array(
    'label'  => 'Verify Password:', 
    'required' => true, 
    'validators' => array(
     array('identical', true, array('password')) 
    ) 
)); 
+0

我收到錯誤:令牌「密碼」與給定的令牌「testpassword」不匹配。 testpassword是值,密碼是在這裏指定的元素的名稱:'相同',真,數組('密碼') – Andrew 2009-10-27 04:38:05

+0

我剛纔說它是奇怪的,它顯示了我輸入的密碼的值錯誤信息,但事實並非如此,因爲我已將元素類型設置爲文本而不是密碼。 – Andrew 2009-10-27 04:40:56

0

我能得到它用下面的代碼工作:

在我的表單中,我只在第二個元素上添加了相同的驗證器:

$this->addElement('text', 'email', array(
     'label'  => 'Email address:', 
     'required' => true, 
     'filters' => array('StringTrim'), 
     'validators' => array('EmailAddress') 
    )); 

$this->addElement('text', 'verify_email', array(
     'label'  => 'Verify Email:', 
     'required' => true, 
     'filters' => array('StringTrim'), 
     'validators' => array('EmailAddress', 'Identical') 
    )); 

和Controller,只是打電話isValid()前:

$validator = $form->getElement('verify_email')->getValidator('identical'); 
$validator->setToken($this->_request->getPost('email')); 

我不知道是否有這樣做,而不必將此代碼添加到控制器的一個更優雅的方式。讓我知道是否有更好的方法來做到這一點。

1
$token = Zend_Controller_Front::getInstance()->getRequest()->getPost('password'); 
$confirmPassword->addValidator(new Zend_Validate_Identical(trim($token))) 
        ->addFilter(new Zend_Filter_StringTrim()) 
        ->isRequired(); 

在擴展zend_form的類中使用上面的代碼。

+0

我們不能使用$ password-> getValue()? 以及我已經嘗試,但我有空值..爲什麼這樣?如果getValue是該元素的方法,那麼它應該返回密碼的值? – 2013-09-05 12:41:04

+0

,因爲getValue()在$ form-> isValid($ _ POST)之前被調用;因爲該代碼在構造函數或init方法內部。 – 2013-09-05 12:49:07

2

兩天後,我找到了正確的答案跟着我一步一步:

第1步:

創建PasswordConfirmation.php文件在項目的根目錄下有此路徑: yourproject/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['user_password']) 
       && ($value == $context['user_password'])) 
      { 
       return true; 
      } 
     } 
     elseif (is_string($context) && ($value == $context)) { 
      return true; 
     } 

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

步驟2:

在表單中添加兩個領域是這樣的:

//create the form elements user_password 
$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', 'user_password_confirm'); 
$userPasswordRepeat->setLabel('Password repeat: '); 
$userPasswordRepeat->setRequired('true'); 
$userPasswordRepeat->addPrefixPath('My_Validate', 'My/Validate', 'validate'); 
$userPasswordRepeat->addValidator('PasswordConfirmation', true, array('user_password')); 
$this->addElement($userPasswordRepeat); 

現在享受你的代碼

+0

@moinsam,如果我有編輯/更新選項以及添加應該做什麼更改? – ehp 2012-09-21 19:00:44

0

隨着Zend框架1.10驗證使用Zend的形式兩個領域的平等所需的代碼Zend Validate是:

$form->addElement('PasswordTextBox', 
         'password', 
         array('label'  => 'Password') 
        ); 

    $form->addElement('PasswordTextBox', 
         'password_confirm', 
         array('label'  => 'Confirm password', 
          'validators' => array(array('Identical', false, 'password')), 
          ) 
        ); 

您可以注意到,在password_confirm元素的驗證器數組中,Identical驗證器作爲數組傳遞,該數組的語義爲:i)驗證器名稱,ii)發生故障時的斷鏈,iii)驗證器選項 如您所見,它是可能傳遞字段名稱而不是檢索值。