Grails日期约束

时间:2011-01-04 14:01:05

标签: validation grails groovy

我有一个带有约束的域类,允许用户至少18岁。

这是有效的:

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

但为什么这不起作用?

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

以前实际上是返回java.util.Date类型的对象。

或者最好的当然是:

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

2 个答案:

答案 0 :(得分:0)

查看MagicNumbers plugin并使用 18.years.ago.toDate()

答案 1 :(得分:0)

类型错误。请注意,您的日期为java.sql.Date,而不是java.util.Date

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

这是一个丑陋的小实现细节。在你的约束中试试这个:

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

如果你混合TimeCategory,例如在BootStrap.groovy中,您将能够编写第二个版本。 mixin可以像这样完成:

Integer.metaClass.mixin(groovy.time.TimeCategory)
相关问题