Zend表单同时验证同一字段的多个自定义验证消息

时间:2013-09-13 11:15:03

标签: forms validation zend-framework message

我有一个Zend表单密码自定义验证。

我已将addValidator函数IInd参数设置为false,因为我需要立即获取所有错误。我的两个验证类都将self::INVLID设置为相应的错误消息。

但是当我查看Zend控制器时,即使两个验证都失败了,我只得到一条(最后一条)错误信息。

我一次需要所有错误消息。

    /* Form validation*/
    $passwordSpecialValidator = new Passwordspecialvalidationvalidator();
    $passwordHistoryValidator = new Passwordhistorylvalidationvalidator(new model(), $username, $newpass);

    $this->newpass->addValidator($passwordSpecialValidator, false)
            ->addValidator($passwordHistoryValidator, false);

    /* One validation class */

    class Passwordhistorylvalidationvalidator extends Zend_Validate_Abstract {

        const INVLID = '';

        protected $_messageTemplates = array(
            self::INVLID => 'Your password doesnt meet the history requirements'
        );

        public function __construct($model, $username, $password) {
            $this->_model = $model;
            $this->_username = $username;
            $this->_password = $password;
        }

        public function isValid($value, $context = null) {

            if ($this->_username == "") {
                $auth = Zend_Auth::getInstance();
                if ($auth->hasIdentity()) {
                    $arrayResult = $auth->getIdentity();
                    if (isset($arrayResult->username)) {
                        $this->_username = $arrayResult->username;
                    }
                }
            }
            $passwordExists = false;
            $oldPasswords = $this->_model->getHistoryPasswords($this->_username, $this->_password);

            if (count($oldPasswords) > 0) {
                $passwordExists = true;
            }

            if ($passwordExists == false) {
                return true;
            } else {
                $this->_setValue($value);
                $this->_error(self::INVLID);
                return false;
            }
        }


    }
    /* Getting error messages */
    foreach ($objForm->getMessages() as $messages) {
        foreach ($messages as $message) {
            $errMessages[] = $message;
        }
    }

但是数组$errMessages只有最后一条验证消息(没有索引),即使特殊验证和历史验证都失败了。如果两个验证都失败,我将如何在数组中获取这两个错误消息?

0 个答案:

没有答案