在我的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)
但validate()
和/或true
为空时,start
仍会返回end
。如果我将这些约束添加到CalendarEventCommand
static constraints = {
start nullable: false
end nullable: false
}
然后validate()
返回false
,但是AFAIK我不需要添加这些约束。
答案 0 :(得分:2)
我认为这是一种预期的行为。关于此功能存在几个JIRA缺陷,其中GRAILS-7431和GRAILS-8583似乎更关注行为。
我正在经历DefaultConstraintEvaluator.java,它只负责域类的全局约束。我想我们最终必须采用后面提到的方式。
static constraints = {
start nullable: false
end nullable: false
}