Grails默认可为空约束

时间:2014-03-31 20:37:33

标签: validation grails gorm

在我的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)
}

如果为startend验证通道创建了一个空值,但我不希望这样做,因为AFAIK应该应用nullable: false的默认约束所有财产。我已经尝试通过将第一个默认约束更改为

来明确添加它
'*'(maxSize: 191, nullable: false)

validate()和/或true为空时,start仍会返回end。如果我将这些约束添加到CalendarEventCommand

static constraints = {
    start nullable: false
    end nullable: false
}

然后validate()返回false,但是AFAIK我不需要添加这些约束。

1 个答案:

答案 0 :(得分:2)

我认为这是一种预期的行为。关于此功能存在几个JIRA缺陷,其中GRAILS-7431GRAILS-8583似乎更关注行为。

我正在经历DefaultConstraintEvaluator.java,它只负责域类的全局约束。我想我们最终必须采用后面提到的方式。

static constraints = {
    start nullable: false
    end nullable: false
}