0
我试图设置使用jQuery Datepicker的人的出生日期。但是,我所得到的是Property dateOfBirth must be a valid Date
。Grails - 域对象无法正确验证
因此,原来,我的控制器看起来是这样的:
def update(Person personInstance) {
if (personInstance == null) {
// do Something
return
}
if (personInstance.hasErrors()) {
respond personInstance.errors, view: 'edit'
return
}
// do the rest
}
我想通了,这与jQuery我应该以产生一个适当的Date
对象使用SimpleDateFormat
对象。尽管如此,即使我直接将一个新的Date
对象分配给dateOfBirth
并随后验证了域对象,如下面的代码段所示 - 我仍然收到Property dateOfBirth must be a valid Date
错误。
def update(Person personInstance) {
if (personInstance == null) {
// do Something
return
}
// added code
personInstance.dateOfBirth = new Date()
personInstance.validate()
// added code
if (personInstance.hasErrors()) {
respond personInstance.errors, view: 'edit'
return
}
// do the rest
}
谢谢你的任何帮助:)
您仍然看到错误的原因是因为在绑定您的命令/域对象后自动调用验证。在手动调用'personInstance.validate()'之前使用'personInstance.clearErrors()'。你可以在文档中看到更多关于这个的内容:http://grails.org/doc/2.2.x/ref/Domain%20Classes/clearErrors.html – 2014-09-01 08:27:14
谢谢你的提示:)现在它完美的工作! – gabriel 2014-09-01 08:48:56
我应该添加一个答案吗? – 2014-09-01 09:06:33