如何在Zend \ Form中自定义正则表达式验证消息,使它们可以重用?

时间:2016-11-09 22:25:39

标签: regex zend-framework2 zend-form zend-validate zend-inputfilter

Regex的默认验证错误消息是

"The input does not match against pattern '%pattern%'"

我可以用像

这样的自定义替换它
"Please make an input according to the pattern '%pattern%'"

但是,我仍然有一个内部正则表达式的用户友好消息。我也可以写

"Only capital letters are allowed"

但在这种情况下,我需要为每个正则表达式字段写一条新消息。

是否可能/如何创建自己的,但仍然灵活/可重复使用/可参数化的消息?

我想要的一个例子:

public function getInputFilterSpecification()
{
    return [
        'foo' => [
            'validators' => [
                [
                    'name' => 'Regex',
                    'options' => [
                        'pattern' => '/^[A-Z0-9:]*$/',
                        'pattern_user_friendly' => 'capital letters, numbers, and colons',
                        'message' => 'The input may only contain the following characters: %pattern_user_friendly%.'
                    ]
                ],
            ]
        ],
        'bar' => [
            'validators' => [
                [
                    'name' => 'Regex',
                    'options' => [
                        // new pattern
                        'pattern' => '/^[A-Z~:\\\\]*$/',
                        // new user friendly pattern description
                        'pattern_user_friendly' => 'capital letters, tildes, colons, and backslashes',
                        // still the same message
                        'message' => 'The input may only contain the following characters: %pattern_user_friendly%.'
                    ]
                ],
            ]
        ],
    ];
}

1 个答案:

答案 0 :(得分:0)

解决方案是创建自定义Validator(扩展Regex),在messageVariables列表中扩展,并添加用于将值设置为属性的逻辑:

class Regex extends ZendRegex
{
    protected $patternUserFriendly;

    public function __construct($pattern)
    {
        // s. https://github.com/zendframework/zend-validator/blob/master/src/Regex.php#L34-L36
        $this->messageVariables['patternUserFriendly'] = 'patternUserFriendly';
        $this->messageTemplates[self::NOT_MATCH] =
            'The input may only contain the following characters: %patternUserFriendly%.'
        ;
        parent::__construct($pattern);
        if (array_key_exists('patternUserFriendly', $pattern)) {
            $this->patternUserFriendly = $pattern['patternUserFriendly'];
        }
    }
}

class MyFieldset extends ZendFieldset implements InputFilterProviderInterface
{
    ...
    public function init()
    {
        parent::init();
        $this->add(
            [
                'type' => 'text',
                'name' => 'foo',
                'options' => [
                    'label' => _('foo')
                ]
            ]);
        $this->add(
            [
                'type' => 'text',
                'name' => 'bar',
                'options' => [
                    'label' => _('bar')
                ]
            ]);
    }
    public function getInputFilterSpecification()
    {
        return [
            'bar' => [
                'validators' => [
                    [
                        'name' => 'MyNamespace\Validator\Regex',
                        'options' => [
                            'pattern' => '/^[a-zA-z]*$/',
                            'patternUserFriendly' => '"a-z", "A-Z"'
                        ]
                    ]
                ]
            ]
        ];
    }
}