【发布时间】:2014-03-31 20:37:32
【问题描述】:
在我的 Grails 应用程序中,我有以下命令对象
@Validateable
class CalendarEventCommand {
@BindingFormat('FestivalType')
Collection<FestivalType> types
Date start
Date end
MapFocalPoint location
boolean freeOnly = false
}
用作控制器动作的参数
def getCalendarEvents(CalendarEventCommand calendarEventCommand) {
if (calendarEventCommand.validate()) {
log.error "Command errors $calendarEventCommand.errors"
} else {
log.warn "Everything is fine"
}
}
在Config.groovy 中,我将以下内容指定为默认约束
grails.gorm.default.constraints = {
// apply a max size of 191 chars to String columns to support utf8mb4
// http://mathiasbynens.be/notes/mysql-utf8mb4
'*'(maxSize: 191)
// this shared constraint provides a way to override the default above for long text properties
unlimitedSize(maxSize: Integer.MAX_VALUE)
}
如果使用start 和end 的空值创建一个实例,则验证通过,但我不希望它这样做,因为AFAIK 默认约束nullable: false 应该应用于所有属性。我已经尝试通过将第一个默认约束更改为
'*'(maxSize: 191, nullable: false)
但是当start 和/或end 为空时,validate() 仍然返回true。如果我将这些约束添加到CalendarEventCommand
static constraints = {
start nullable: false
end nullable: false
}
然后validate() 返回false,但据我所知,我不需要添加这些约束。
【问题讨论】:
标签: validation grails grails-orm