2014-03-31 69 views
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创建实例对于startend来说是有效的,但我不会期望它,因为AFAIK应该将默认约束nullable: false应用于所有属性。我试着明确添加此,通过改变第一默认约束

'*'(maxSize: 191, nullable: false) 

validate()仍返回truestart和/或end为空。如果我添加这些约束CalendarEventCommand

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

然后validate()回报false,但据我所知,我添加这些约束它不应该是必要的。

回答

2

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

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

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