2008-12-07 35 views
7

我已經創建了一個表單來將用戶添加到數據庫並使用戶可用於登錄。Zend_Form:如何檢查2個字段是否相同

現在我有兩個密碼字段(第二個用於驗證第一個)。我怎樣才能將這種驗證的驗證器添加到zend_form?

這是我的兩個密碼字段代碼:

$password = new Zend_Form_Element_Password('password', array(
     'validators'=> array(
      'Alnum', 
      array('StringLength', array(6,20)) 
      ), 
     'filters' => array('StringTrim'), 
     'label'  => 'Wachtwoord:' 
     )); 

    $password->addFilter(new Ivo_Filters_Sha1Filter()); 

    $password2 = new Zend_Form_Element_Password('password', array(
     'validators'=> array(
      'Alnum', 
      array('StringLength', array(6,20)) 
      ), 
     'filters' => array('StringTrim'), 
     'required' => true, 
     'label'  => 'Wachtwoord:' 
     )); 
    $password2->addFilter(new Ivo_Filters_Sha1Filter()); 
+1

然同樣的事情 - 事實證明Zend_Validate_Identical(不在周圍,當這張貼我猜)將採取另一個元素的名稱來檢查:http://stackoverflow.com/questions/347856/zend-form-how-to- check-2-fields-are-same/3782388#3782388 – 2010-09-23 20:44:24

回答

3

當我在尋找相同的時候,我發現這個工作非常好的通用驗證器的相同字段。我現在不找,所以我剛剛張貼代碼...

<?php 

class Zend_Validate_IdenticalField extends Zend_Validate_Abstract { 
    const NOT_MATCH = 'notMatch'; 
    const MISSING_FIELD_NAME = 'missingFieldName'; 
    const INVALID_FIELD_NAME = 'invalidFieldName'; 

    /** 
    * @var array 
    */ 
    protected $_messageTemplates = array(
    self::MISSING_FIELD_NAME => 
     'DEVELOPMENT ERROR: Field name to match against was not provided.', 
    self::INVALID_FIELD_NAME => 
     'DEVELOPMENT ERROR: The field "%fieldName%" was not provided to match against.', 
    self::NOT_MATCH => 
     'Does not match %fieldTitle%.' 
); 

    /** 
    * @var array 
    */ 
    protected $_messageVariables = array(
    'fieldName' => '_fieldName', 
    'fieldTitle' => '_fieldTitle' 
); 

    /** 
    * Name of the field as it appear in the $context array. 
    * 
    * @var string 
    */ 
    protected $_fieldName; 

    /** 
    * Title of the field to display in an error message. 
    * 
    * If evaluates to false then will be set to $this->_fieldName. 
    * 
    * @var string 
    */ 
    protected $_fieldTitle; 

    /** 
    * Sets validator options 
    * 
    * @param string $fieldName 
    * @param string $fieldTitle 
    * @return void 
    */ 
    public function __construct($fieldName, $fieldTitle = null) { 
    $this->setFieldName($fieldName); 
    $this->setFieldTitle($fieldTitle); 
    } 

    /** 
    * Returns the field name. 
    * 
    * @return string 
    */ 
    public function getFieldName() { 
    return $this->_fieldName; 
    } 

    /** 
    * Sets the field name. 
    * 
    * @param string $fieldName 
    * @return Zend_Validate_IdenticalField Provides a fluent interface 
    */ 
    public function setFieldName($fieldName) { 
    $this->_fieldName = $fieldName; 
    return $this; 
    } 

    /** 
    * Returns the field title. 
    * 
    * @return integer 
    */ 
    public function getFieldTitle() { 
    return $this->_fieldTitle; 
    } 

    /** 
    * Sets the field title. 
    * 
    * @param string:null $fieldTitle 
    * @return Zend_Validate_IdenticalField Provides a fluent interface 
    */ 
    public function setFieldTitle($fieldTitle = null) { 
    $this->_fieldTitle = $fieldTitle ? $fieldTitle : $this->_fieldName; 
    return $this; 
    } 

    /** 
    * Defined by Zend_Validate_Interface 
    * 
    * Returns true if and only if a field name has been set, the field name is available in the 
    * context, and the value of that field name matches the provided value. 
    * 
    * @param string $value 
    * 
    * @return boolean 
    */ 
    public function isValid($value, $context = null) { 
    $this->_setValue($value); 
    $field = $this->getFieldName(); 

    if (empty($field)) { 
     $this->_error(self::MISSING_FIELD_NAME); 
     return false; 
    } elseif (!isset($context[$field])) { 
     $this->_error(self::INVALID_FIELD_NAME); 
     return false; 
    } elseif (is_array($context)) { 
     if ($value == $context[$field]) { 
     return true; 
     } 
    } elseif (is_string($context) && ($value == $context)) { 
     return true; 
    } 
    $this->_error(self::NOT_MATCH); 
    return false; 
    } 
} 
?> 
1

這裏是我做到了這一點:)

創建第一遍然後輸入第二個箱子通過輸入並添加相同的驗證與以前的數據密碼輸入。

$password_2->addValidator('identical', false, $this->_request->getPost('password')); 
+0

只有在控制器中添加了驗證器後,這才起作用。這在表單內不起作用,因爲您無法訪問請求。 – Andrew 2009-12-17 06:35:04

+0

這是不好的做法,因爲控制器現在負責添加驗證標準。 – 2010-09-09 17:25:22

40

Zend_Validate的當前版本有這個建於 - 同時也有很多其他的答案,似乎所有需要傳遞一個值Zend_Validate_Identical。雖然可能需要一點,但現在可以傳遞另一個元素的名稱。

Zend_Validate section of the reference guide

Zend_Validate_Identical還支持形式的元素的比較。這可以通過使用元素的名稱作爲標記來完成。請看下面的例子:

$form->addElement('password', 'elementOne'); 
$form->addElement('password', 'elementTwo', array(
    'validators' => array(
     array('identical', false, array('token' => 'elementOne')) 
    ) 
)); 

通過使用元素名稱從作爲令牌用於第二元件中的第一元件,如果第二元件是與所述第一元件相同的驗證器驗證。如果你的用戶沒有輸入兩個相同的值,你會得到一個驗證錯誤。

0

您可以驗證訪問所有表單域, 也可以使用構造函數來傳遞額外的參數

class Example_Validator extends Zend_Validate_Abstract{ 

const NOT_IDENTICALL = 'not same'; 

private $testValue;  

public function __construct($arg) { 
     $this->testValue = $arg;  
    } 

protected $_messageTemplates = array(
    self::NOT_IDENTICALL => "Passwords aren't same" 
);  

public function isValid($value, $context = null) 
{ 
    echo $context['password']; 
    echo '<br>'; 
    echo $this->testValue; 

    return true; 
} 
} 

調用這個驗證過這個舊的問題尋找

$form = new Zend_Form(); 
$form->setAction('success'); 
$form->setMethod('post'); 
$form->addElement('text', 'username'); 
$usernameElement = $form->getElement('username'); 
$form->addElement('password', 'password'); 
$passwordElement = $form->getElement('password'); 
$myValidator2 = new Example_Validator("Hello !"); 
$passwordElement->addValidator($myValidator2, true);  
$form->addElement('submit', 'submit'); 
$submitButton = $form->getElement('submit');