Grails Validation Gotcha on Dates

页面时间格式是02/23/2011

代码如下

def application = new Application(params)
if (params?.accidentDate) {
   application.accidentDate = new Date().parse("MM/dd/yyyy", params.accidentDate)
} else {
   application.accidentDate = null
}
application.validate()
if (!application.hasErrors()){
   application.save(failOnError:true)
}

但是还是报错

应该是在defapplication=newApplication(params)的时候就已经有错误信息存在了

解决方法

def submitApplication(def params) {
        def application = new Application(params)
        application.clearErrors()
        if (params?.accidentDate) {
            application.accidentDate = new Date().parse("MM/dd/yyyy", params.accidentDate)
        } else {
            application.accidentDate = null
        }

        if (!application.hasErrors()){
            application.save(failOnError:true)
        }
        application
    }

转自:http://jts-blog.com/?p=9269

相关推荐