【问题标题】:Grails default nullable constraintsGrails 默认可为空的约束
【发布时间】:2014-03-31 20:37:32
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: validation grails grails-orm


    【解决方案1】:

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

    我正在经历DefaultConstraintEvaluator.java,它只处理域类的全局约束。我认为我们最终必须使用后面提到的方式。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      • 2012-06-18
      相关资源
      最近更新 更多