2014-03-31 68 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 
}