A simple grails custom validator

While writing a simple CRUD with #grails, client asked me to validate pogo’s birth date (had to be in the last year). In this cases, we can’t just use regular validators, cause their are static. So we can solve this using our own custom validators, so simple and useful.

static constraints = {
	//...
	borned(validator: {
		return (it > new Date()-365)
	})
	//...
}

That’s it, this way every time a new instance is validated (during save or manually), a new date will be created and compared to it. (no I don’t care about leap years).

Already following me on twitter?

[]s,

3 Comments so far

  1. Twitted by snaglepus on August 10th, 2009

    [...] This post was Twitted by snaglepus [...]

  2. darxriggs on August 11th, 2009

    You can reduce the code for better readability with Groovy’s strength to:

    born validator: { it > new Date() – 365 }

  3. lucastex on August 11th, 2009

    @darxriggs, Perfect!!

Leave a Reply

Web Analytics