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
,但據我所知,我添加這些約束它不應該是必要的。