2008-12-07 70 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');