Zend Framework 2 - 注释表单、RegEx 验证和自定义错误消息 [英] Zend Framework 2 - Annotation form, RegEx validation and custom error messages
问题描述
如此处所述,我正在使用 GeoPositionFields
.因为 Zend 不支持这一点,所以我使用了标准的 RegEx
验证器.
As outlined here I'm working with GeoPositionFields
. Because this is not supported from Zend, I went with a standard RegEx
validator.
效果很好,但我仍然需要自定义错误消息 - 如何实现这一点有什么想法吗?
It works great but I still need a custom error message - any ideas how to achieve this?
我的例子中的那个什么都不做...
The one in my example just does nothing...
/**
* @ORM\Column(type="string")
* @Form\Filter({"name":"StringTrim"})
* @Form\Validator({"name":"Regex", "options":{"pattern":"/(-?\d{1,3}\D\d+)[^\d-]+(-?\d{1,3}\D\d+)/"}})
* @Form\ErrorMessage("My custom message")
* @Form\Attributes({"type":"text"})
* @Form\Options({"label":"GeoPos"})
*
*/
protected $geopoint;
即使是这个也只是被忽略了:
Even this one is just beeing ignored:
@Form\Messages({"regexNotMatch": "My custom message"})
推荐答案
您必须使用选项中的 messages
键覆盖默认消息.
You will have to overwrite the default messages using the messages
key from the options.
试试这个(我想你必须把它剪成一行才能使注释起作用;)这取决于你,呵呵.
Try this (i guess you'll have to trim this into one line though for the annotations to work ;) That's up to you, hehe.
@Form\Validator({
"name":"regex",
"options":{
"pattern":"/(-?\d{1,3}\D\d+)[^\d-]+(-?\d{1,3}\D\d+)/",
"messages":{
"regexInvalid":"Regex is invalid, Booo!",
"regexNotMatch": "Input doesn't match, bleeeeh!",
"regexErrorous": "Internal error, i'm like wtf!"
}
}
})
每个验证器都有自己的消息.最好建议您查看源代码以找出每个元素中存在哪些错误消息.举个例子,请按照 this链接(单击) 以查看如何了解消息键.
Each Validator has it's own messages. You're best advised to see the Source-Code to find out what ErrorMessages are present within each Element. To give you an example, please follow this link (click) to see how to find out about the messages-keys.
当使用 array-style-syntax 在 Annotations 之外创建表单时,对像这样的键使用 statis 方法可能更安全一些
When using the array-style-syntax for creating forms outside of Annotations, it may be a little bit safer to go the statis approach for the keys like
'messages' => array(
\Zend\Validator\Regex::INVALID => "Regex is invalid, Booo!",
//etc...
)
因为字符串 - 至少在理论上 - 总是会改变,常量不会.
Since strings - at least in theory - could always change, the constants won't.
这篇关于Zend Framework 2 - 注释表单、RegEx 验证和自定义错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!