我目前正在开发 Grails 应用程序,我想更改为我的域模型中的对象显示的默认错误消息。我相信我所做的一切都是正确的,但也许有人可以帮我解决这个问题,代码如下:
领域模型:
class Details {
long mobileNo
String name
static constraints = {
mobileNo(blank:false, maxsize:12, matches:"44[0-9]{10}")
}
}
消息属性
sms.mobileNo.matches.invalid=You must enter a correct mobile number in {0}
看法
<g:hasErrors bean="${detailsInstance}">
<div class="alert error">
<g:renderErrors bean="${detailsInstance}" as="list" />
</div>
</g:hasErrors>
现在,当单击页面上的创建时,它会转到以下控制器功能:
def details = new Details(params)
if (details.validate()) {
}
else{
render view: 'create', model: [detailsInstance: details]
}
现在我希望应用程序可以像这样工作:一个错误的数据被传递给控制器,它认为它是无效的,然后将有错误的模型传递回同一个视图。然后从messages.properties文件中提取数据不正确的错误消息。但是,我希望在哪里看到:
You must enter a correct mobile number in [mobileNo]
相反,我看到了这个:
Failed to convert property value of type java.lang.String to required type long for property mobileNo; nested exception is java.lang.IllegalArgumentException: Could not parse number: Unparseable number: "gg"
有人可以帮我生成自定义消息以进行验证,以便向用户显示有用的消息吗?谢谢