Zend验证 - 多个值

时间:2011-12-08 16:14:44

标签: php zend-framework zend-validate

我想验证一个允许包含多个电子邮件地址的表单元素,用逗号和空格分隔。

这是我的代码:

class My_Validate_EmailAddresses extends Zend_Validate_Abstract
{
    const MSG_INVALID = 'msgInvalid';

    /** @var Zend_Validate_EmailAddress */
    protected static $_validatorEmailAddress;

    /** @var array */
    protected $_messageTemplates = array(
        self::MSG_INVALID => "'%value%' is not a valid email address",
    );

    /**
     *
     * @return Zend_Validate_EmailAddress
     */
    public static function getValidatorEmailAddress()
    {
        if (is_null(self::$_validatorEmailAddress)) {
            self::$_validatorEmailAddress = new Zend_Validate_EmailAddress();
        }
        return self::$_validatorEmailAddress;
    }

    /**
     * Values passed in here should be filterd by My_Filter_CommaSpaceSeparated first
     *
     * @param string $value
     * @return boolean
     */
    public function isValid($value)
    {
        $valid = true;
        $emails = explode(', ', $value);
        foreach ($emails as $email) {
            if (!self::getValidatorEmailAddress()->isValid($email)) {
                $this->_error(self::MSG_INVALID, $email);
                $valid = false;
            }
        }
        return $valid;
    }
}

它正确验证,但如果列表中有多个未验证的电子邮件地址,则仅显示最终错误消息。我希望看到每个无效电子邮件地址的消息。

2 个答案:

答案 0 :(得分:3)

我通过在$_messageTemplates数组中使用电子邮件地址作为消息密钥解决了多个消息问题。我还创建了一个电子邮件验证类,然后将其扩展为多个验证。

单一电子邮件地址

class My_Validate_EmailAddress extends Zend_Validate_Abstract
{
    const MSG_INVALID = "'%value%' is not a valid email address";

    /** @var Zend_Validate_EmailAddress */
    protected static $_validatorEmailAddress;

    /** @var array */
    protected $_messageTemplates = array();

    /**
     *
     * @return Zend_Validate_EmailAddress
     */
    public static function getValidatorEmailAddress()
    {
        if (is_null(self::$_validatorEmailAddress)) {
            self::$_validatorEmailAddress = new Zend_Validate_EmailAddress();
        }
        return self::$_validatorEmailAddress;
    }

    /**
     *
     * @param string $value
     * @return boolean
     */
    public function isValid($value)
    {
        $valid = true;
        if (!self::getValidatorEmailAddress()->isValid($value)) {
            $this->_messageTemplates[$value] = self::MSG_INVALID;
            $this->_error($value, $value);
            $valid = false;
        }
        return $valid;
    }
}

多个电子邮件地址

class My_Validate_EmailAddresses extends My_Validate_EmailAddress
{
    /**
     * Values passed in here should be filterd by My_Filter_CommaSpaceSeparated first
     *
     * @param string $value
     * @return boolean
     */
    public function isValid($value)
    {
        $valid = true;
        $emails = explode(', ', $value);
        foreach ($emails as $email) {
            if (!parent::isValid($email)) {
                $valid = false;
            }
        }
        return $valid;
    }
}

逗号和空间过滤器

class My_Filter_CommaSpaceSeparated implements Zend_Filter_Interface
{
    /**
     *
     * @param string $value
     * @return string
     */
    public function filter($value)
    {
        // normalize delimiters
        $value = str_replace(array(' ', ';'), ',', $value);
        // explode values
        $values = explode(',', $value);
        // remove empty values
        $values = array_filter($values);
        // implode
        $value = implode(', ', $values);
        // return filtered value
        return $value;
    }
}

答案 1 :(得分:2)

由于$ this-> _error()方法的当前实现,你无法做到这一点。 但是您可以将所有未验证的电子邮件放入字符串中,然后将其传递给_error()。

    $wrong_emails = array ();
    foreach ($emails as $email) {
         if (!self::getValidatorEmailAddress()->isValid($email)) {
               $wrong_emails [] = $email;
         }
    }
    if ($wrong_emails)
    {
         $this->_error(self::MSG_INVALID, join (', ', $wrong_emails);
         return false;
    }

return true;