<tfoot id='IjvSO'></tfoot>

      <legend id='IjvSO'><style id='IjvSO'><dir id='IjvSO'><q id='IjvSO'></q></dir></style></legend>

      <small id='IjvSO'></small><noframes id='IjvSO'>

      1. <i id='IjvSO'><tr id='IjvSO'><dt id='IjvSO'><q id='IjvSO'><span id='IjvSO'><b id='IjvSO'><form id='IjvSO'><ins id='IjvSO'></ins><ul id='IjvSO'></ul><sub id='IjvSO'></sub></form><legend id='IjvSO'></legend><bdo id='IjvSO'><pre id='IjvSO'><center id='IjvSO'></center></pre></bdo></b><th id='IjvSO'></th></span></q></dt></tr></i><div id='IjvSO'><tfoot id='IjvSO'></tfoot><dl id='IjvSO'><fieldset id='IjvSO'></fieldset></dl></div>
          <bdo id='IjvSO'></bdo><ul id='IjvSO'></ul>

        Zend_Form:如何检查 2 个字段是否相同

        时间:2023-05-31

        <i id='0jC1q'><tr id='0jC1q'><dt id='0jC1q'><q id='0jC1q'><span id='0jC1q'><b id='0jC1q'><form id='0jC1q'><ins id='0jC1q'></ins><ul id='0jC1q'></ul><sub id='0jC1q'></sub></form><legend id='0jC1q'></legend><bdo id='0jC1q'><pre id='0jC1q'><center id='0jC1q'></center></pre></bdo></b><th id='0jC1q'></th></span></q></dt></tr></i><div id='0jC1q'><tfoot id='0jC1q'></tfoot><dl id='0jC1q'><fieldset id='0jC1q'></fieldset></dl></div>

        • <small id='0jC1q'></small><noframes id='0jC1q'>

          <legend id='0jC1q'><style id='0jC1q'><dir id='0jC1q'><q id='0jC1q'></q></dir></style></legend>

              <tbody id='0jC1q'></tbody>
          • <tfoot id='0jC1q'></tfoot>
              <bdo id='0jC1q'></bdo><ul id='0jC1q'></ul>

                  本文介绍了Zend_Form:如何检查 2 个字段是否相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  I have created a form to add a user to a database and make user available for login.

                  现在我有两个密码字段(第二个用于验证第一个).如何向 zend_form 添加用于此类验证的验证器?

                  Now I have two password fields (the second is for validation of the first). How can I add a validator for this kind of validation to zend_form?

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

                  This is my code for the two password fields:

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

                  推荐答案

                  当我在寻找相同的东西时,我发现这个非常有效的通用 Validator for Identical Fields.我现在找不到它所以我只是发布代码...

                  When I was looking for the same, I found this very well working generic Validator for Identical Fields. I don't find it now so I just post the code...

                  <?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_Form:如何检查 2 个字段是否相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将 PHP 数组编码为 JSON 数组,而不是对象? 下一篇:Zend 邮件 Gmail SMTP

                  相关文章

                1. <i id='lXWWF'><tr id='lXWWF'><dt id='lXWWF'><q id='lXWWF'><span id='lXWWF'><b id='lXWWF'><form id='lXWWF'><ins id='lXWWF'></ins><ul id='lXWWF'></ul><sub id='lXWWF'></sub></form><legend id='lXWWF'></legend><bdo id='lXWWF'><pre id='lXWWF'><center id='lXWWF'></center></pre></bdo></b><th id='lXWWF'></th></span></q></dt></tr></i><div id='lXWWF'><tfoot id='lXWWF'></tfoot><dl id='lXWWF'><fieldset id='lXWWF'></fieldset></dl></div>
                2. <tfoot id='lXWWF'></tfoot>

                    <bdo id='lXWWF'></bdo><ul id='lXWWF'></ul>

                    <small id='lXWWF'></small><noframes id='lXWWF'>

                  1. <legend id='lXWWF'><style id='lXWWF'><dir id='lXWWF'><q id='lXWWF'></q></dir></style></legend>