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)
}


但是还是报错

应该是在def application = new Application(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

你可能感兴趣的:(grails)