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%.'
]
],
]
],
];
}
這是不是有點大材小用,以建立自己的正則表達式驗證只是爲了顯示你'patternUse設置字符串rFriendly',而你可能剛剛覆蓋了這條消息,並在你的消息中使用了'patternUserFriendly'的值? – Kwido
感謝您的輸入!我剛剛更新了代碼,以避免每次都編寫相同的消息。但是你是對的,對一行代碼進行優化可能會花費很多精力。在許多類似'Regex'驗證字段的情況下,只有在驗證消息的'pattern'部分纔會有所不同。 – automatix