Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Zend_表单:如何检查两个字段是否相同_Php_Validation_Zend Framework_Zend Form - Fatal编程技术网

Php Zend_表单:如何检查两个字段是否相同

Php Zend_表单:如何检查两个字段是否相同,php,validation,zend-framework,zend-form,Php,Validation,Zend Framework,Zend Form,我创建了一个表单,将用户添加到数据库中,并使用户可以登录 现在我有两个密码字段(第二个用于验证第一个)。如何在zend_表单中添加用于此类验证的验证器 这是我的两个密码字段的代码: $password = new Zend_Form_Element_Password('password', array( 'validators'=> array( 'Alnum', array('StringLength', array

我创建了一个表单,将用户添加到数据库中,并使用户可以登录

现在我有两个密码字段(第二个用于验证第一个)。如何在zend_表单中添加用于此类验证的验证器

这是我的两个密码字段的代码:

    $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());
我是这样做的:)

创建第一个密码输入,然后装箱第二个密码输入,并添加与先前密码输入数据相同的验证器

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

当我寻找相同的字段时,我发现这个通用验证器对于相同的字段工作得非常好。我现在找不到,所以我只是发布代码

<?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;
  }
}
?>

当前版本的Zend_Validate内置了此功能-虽然还有很多其他答案,但似乎所有答案都需要向
Zend_Validate_idential
传递一个值。虽然在某一点上可能需要这样做,但现在可以传递另一个元素的名称

从:

Zend_Validate_还支持表单元素的比较。这可以通过使用元素的名称作为标记来实现。请参见以下示例:

通过使用第一个元素的元素名作为第二个元素的标记,验证器验证第二个元素是否与第一个元素相等。如果用户没有输入两个相同的值,则会出现验证错误


您可以从validator访问所有表单字段, 还可以使用构造函数传递其他参数

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;
}
}

只有在控制器中添加了验证器时,这才有效。这在表单中不起作用,因为您将无法访问请求。这是一种不好的做法,因为控制器现在负责添加验证条件。遇到了这个旧问题,寻找相同的东西-结果是Zend_Validate_完全相同(我猜这不在发布时)将使用另一个元素的名称进行检查:非常好的解决方案,我正在删除我发布的答案,因为它现在已经过时了。
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');