开发者

Grails Date Constraints

I have an domain class with a constraint, which allows users to be at least 18 years old.

This is working:

birthday(nullable: false, max:new Date(use(TimeCategory){18.years.ago.getTime()}))

But why is this not working?

birthday(nullable: false, max:(use(开发者_运维百科TimeCategory){18.years.ago}))

Ago is actually returning an object of type java.util.Date.

Or best would be of course this:

birthday(nullable: false, max: 18.years.ago)


Look at MagicNumbers plugin and use 18.years.ago.toDate()


The type is wrong. Note that your date is a java.sql.Date, not a java.util.Date.

use (TimeCategory) {
    assert 18.years.ago.class == java.sql.Date
}

This is an ugly little implementation detail. Try this in your constraint:

birthday(nullable: false, max:(use(TimeCategory){18.years.ago  as Date}))
birthday(nullable: false, max: 18.years.ago  as Date)

If you mixin the TimeCategory, e.g. in BootStrap.groovy, you'll be able to just write the second version. The mixin can be done like so:

Integer.metaClass.mixin(groovy.time.TimeCategory)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜