Grails Validation Gotcha on Dates
Posted by jt - 02/03/11 at 01:03 pmI ran into a little gotcha with Grails and validation today. I was parsing a date from a web post and kept getting a validation error.
I have a ExtJS web form posting a date value as a string:
02/23/2011
This format does not get automagically converted to a date.
My original code was as follows:
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) } |
Fairly simple code. I’m using the parameters map to create a new object. When I got the validation errors, I realized the date format was wrong and added the new date logic.
Seemed simple enough. But I kept getting validation errors on the date.
Field error in object 'com.asf.Application' on field 'accidentDate': rejected value [Wed Feb 23 00:00:00 EST 2011]; codes [typeMismatch.com.asf.Application.accidentDate,typeMismatch.accidentDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [com.asf.Application.accidentDate,accidentDate]; arguments []; default message [accidentDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'accidentDate'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "02/23/2011"]
I banged my head on the desk a few times thinking there was some kind of type mismatch. My domain clearly had a Date property, a I was creating a valid date and setting it. WTF!?! What is not happy about it!
Answer nothing was wrong with with the value I parsed and set. Problem was in creating the application object in the first place. This chunk of code:
def application = new Application(params) |
is implicitly calling validate and populating the errors object. Even though I was setting a valid value and calling validate later, the errors object had already been set and was not getting cleared.
Solution:
application.clearErrors() |
ta-dah! Works like a champ now!
Full code:
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 } |
A bunch of random technology stuff that has my attention. I work with a lot of Oracle, Java, and dabble with various open source software packages.
March 2nd, 2011 at 4:51 pm
[...] posting a date value as a string: 02/23/2011 This format does not get automagically converted… [full post] jt JT's Blog uncategorizedgormgrails 0 0 0 0 0 [...]
July 4th, 2011 at 10:36 am
Hello,
Just wanted to thank you.
You helped me fix a bug that was driving me crazy.
Giancarlo
November 29th, 2012 at 6:21 am
Thank you – I had the same problem here