如概述here我正在使用 GeoPositionFields
。因为 Zend 不支持它,所以我使用了标准的 RegEx
验证器。
效果很好,但我仍然需要自定义错误消息 - 有什么想法可以实现吗?
我的示例中的那个什么都不做...
/**
* @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;
即使是这个也被忽略了:
@Form\Messages({"regexNotMatch": "My custom message"})
最佳答案
您必须使用选项中的 messages
键覆盖默认消息。
试试这个(我猜你必须将它缩减成一行才能使注释起作用;)这取决于你,呵呵。
@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 link (click)查看如何找到有关消息键的信息。
当使用数组样式语法在注解之外创建表单时,对于像这样的键使用统计方法可能会更安全一些
'messages' => array(
\Zend\Validator\Regex::INVALID => "Regex is invalid, Booo!",
//etc...
)
因为字符串——至少在理论上——总是可以改变的,所以常量不会。
关于php - Zend Framework 2 - 注释表单、RegEx 验证和自定义错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14068446/