0

Regex默認驗證錯誤消息是如何在Zend Form中自定義正則表達式驗證消息,保持它們的可重用性?

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

回答

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"' 
         ] 
        ] 
       ] 
      ] 
     ]; 
    } 
} 
+1

這是不是有點大材小用,以建立自己的正則表達式驗證只是爲了顯示你'patternUse設置字符串rFriendly',而你可能剛剛覆蓋了這條消息,並在你的消息中使用了'patternUserFriendly'的值? – Kwido

+0

感謝您的輸入!我剛剛更新了代碼,以避免每次都編寫相同的消息。但是你是對的,對一行代碼進行優化可能會花費很多精力。在許多類似'Regex'驗證字段的情況下,只有在驗證消息的'pattern'部分纔會有所不同。 – automatix