7

ユーザーをデータベースに追加し、ユーザーをログインできるようにするフォームを作成しました。

これで、2 つのパスワード フィールドができました (2 番目は最初の検証用です)。この種の検証用のバリデーターを zend_form に追加するにはどうすればよいですか?

これは、2 つのパスワード フィールドのコードです。

    $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());
4

4 に答える 4

40

Zend_Validate の現在のバージョンにはこれが組み込まれています。他にもたくさんの答えがありますが、すべて に値を渡す必要があるようですZend_Validate_Identical。ある時点でそれが必要だったかもしれませんが、別の要素の名前を渡すことができるようになりました。

Zend_Validateリファレンスガイドのセクションから:

Zend_Validate_Identical は、フォーム要素の比較もサポートしています。これは、要素の名前をトークンとして使用することで実行できます。次の例を参照してください。

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

最初の要素の要素名を 2 番目の要素のトークンとして使用することにより、バリデータは 2 番目の要素が最初の要素と等しいかどうかを検証します。ユーザーが 2 つの同一の値を入力しない場合、検証エラーが発生します。

于 2010-09-23T20:41:03.200 に答える
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;
  }
}
?>
于 2008-12-08T06:37:33.223 に答える
1

これが私がこれを行った方法です:)

最初のパスの入力を作成し、2 番目のパスの入力を作成し、以前のパスワード入力からのデータを使用して同一のバリデータを追加します。

$password_2->addValidator('identical', false, $this->_request->getPost('password'));
于 2008-12-08T06:09:09.833 に答える