Validation Grails自定义验证消息不起作用?
我目前正在处理一个Grails应用程序,我想更改为域模型中的对象显示的默认错误消息。我相信我所做的一切都是正确的,但也许有人能帮我解决这个问题,代码如下: 域模型:Validation Grails自定义验证消息不起作用?,validation,grails,messages,Validation,Grails,Messages,我目前正在处理一个Grails应用程序,我想更改为域模型中的对象显示的默认错误消息。我相信我所做的一切都是正确的,但也许有人能帮我解决这个问题,代码如下: 域模型: class Details { long mobileNo String name static constraints = { mobileNo(blank:false, maxsize:12, matches:"44[0-9]{10}") } } 消息.属性 sms.mobileNo.
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>
现在,我希望应用程序能像这样工作:一个错误的数据被传递给它认为无效的控制器,然后将有错误的模型传递回同一个视图。然后从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"
相反,我看到的是:
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"
有人能帮我生成自定义消息进行验证,以便向用户显示有用的消息吗?谢谢问题是,在验证之前,绑定尝试将您的值“gg”绑定到一个long 你的约束还说:
mobileNo(blank:false, maxsize:12, matches:"44[0-9]{10}"
因此,您希望它是一个字符串,但它被定义为长字符串
尝试将mobileNo更改为字符串类型。深入查看堆栈跟踪,它可能会告诉您引发错误的行号。我的猜测是,它正在以下行抛出异常
new Details(params)
这是因为在grails试图将“gg”转换为long之前,您没有检查输入,这是行不通的。我认为你有四个选择
新详细信息(参数)
之前验证参数
(不推荐)享受吧 非常感谢这是一个很大的帮助,我现在已经整理好了:-D