[英]Grails default nullable constraints
In my Grails app I have the following command object在我的 Grails 应用程序中,我有以下命令对象
@Validateable
class CalendarEventCommand {
@BindingFormat('FestivalType')
Collection<FestivalType> types
Date start
Date end
MapFocalPoint location
boolean freeOnly = false
}
which is used as the argument to a controller action用作控制器操作的参数
def getCalendarEvents(CalendarEventCommand calendarEventCommand) {
if (calendarEventCommand.validate()) {
log.error "Command errors $calendarEventCommand.errors"
} else {
log.warn "Everything is fine"
}
}
In Config.groovy
I've specified the following as the default constraints在
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)
}
If an instance is created with a null value for start
and end
validation passes, but I wouldn't expect it to because AFAIK a default constraint of nullable: false
should be applied to all properties.如果一个实例是为
start
和end
验证通过一个空值创建的,但我不希望它这样做,因为 AFAIK 的默认约束为nullable: false
应该应用于所有属性。 I've tried adding this explicitly, by changing the first default constraint to我已经尝试通过将第一个默认约束更改为
'*'(maxSize: 191, nullable: false)
But validate()
still returns true
when start
and/or end
are null.但是当
start
和/或end
为空时, validate()
仍然返回true
。 If I add these constraints to CalendarEventCommand
如果我将这些约束添加到
CalendarEventCommand
static constraints = {
start nullable: false
end nullable: false
}
then validate()
returns false
, but AFAIK it shouldn't be necessary for me to add these constraints.然后
validate()
返回false
,但是AFAIK我没有必要添加这些约束。
I think this is an expected behavior.我认为这是一种预期的行为。 There are couple of JIRA defects regarding this functionality out of which GRAILS-7431 and GRAILS-8583 seems more focused towards the behavior.
关于此功能有几个 JIRA 缺陷,其中GRAILS-7431和GRAILS-8583似乎更侧重于行为。
I was going through DefaultConstraintEvaluator.java which takes care of global constraints only for domain classes.我正在浏览DefaultConstraintEvaluator.java ,它只处理域类的全局约束。 I think we have to end up using the way mentioned later.
我认为我们最终必须使用后面提到的方式。
static constraints = {
start nullable: false
end nullable: false
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.