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,
Comments(3)
[...] This post was Twitted by snaglepus [...]
You can reduce the code for better readability with Groovy’s strength to:
born validator: { it > new Date() – 365 }
@darxriggs, Perfect!!