如何在 Zend\Form 中自定义 Regex 验证消息以使其可重复使用?

How to customise the Regex validation messages in a Zend\Form keeping them reusable?

Regex 的默认验证错误消息是

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

我可以用自定义的替换它

"Please make an input according to the pattern '%pattern%'"

但是我仍然收到一条不太用户友好的消息,其中包含内部正则表达式。我也会写

"Only capital letters are allowed"

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

是否可能/如何创建自己的,但仍然 flexible/reusable/parameterizable 消息?


我想要的例子:

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%.'
                    ]
                ],
            ]
        ],
    ];
}

解决方案是创建自定义 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"'
                        ]
                    ]
                ]
            ]
        ];
    }
}