2
以我的Grails应用我有以下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)
}
如果使用空v创建实例对于start
和end
来说是有效的,但我不会期望它,因为AFAIK应该将默认约束nullable: false
应用于所有属性。我试着明确添加此,通过改变第一默认约束
'*'(maxSize: 191, nullable: false)
但validate()
仍返回true
时start
和/或end
为空。如果我添加这些约束CalendarEventCommand
static constraints = {
start nullable: false
end nullable: false
}
然后validate()
回报false
,但据我所知,我添加这些约束它不应该是必要的。