我正在使用grails 2.2.1並嘗試驗證嵌套的命令結構。這是我的命令對象的一個簡化版本:grails驗證嵌套的命令對象不能正常工作
@Validateable
class SurveyCommand {
SectionCommand useful
SectionCommand recommend
SurveyCommand() {
useful = new SectionCommand(
question: 'Did you find this useful?',
isRequired: true)
recommend = new SectionCommand(
question: 'Would you recommend to someone else?',
isRequired: false)
}
}
@Validateable
class SectionCommand {
String question
String answer
boolean isRequired
static constraints = {
answer(validator: answerNotBlank, nullable: true)
}
static answerNotBlank = { String val, SectionCommand obj ->
if(obj.isRequired) {
return val != null && !val.isEmpty()
}
}
}
當我嘗試驗證的SurveyCommand
一個實例,它總是返回true
不管段值和SectionCommand
(answerNotBlank
)我自定義的驗證永遠不會被調用。從grails文檔看來,this kind of nested structure is supported(deepValidate
默認爲true)。但是,也許這個規則只適用於域對象而不是Command對象?或者我在這裏錯過了什麼?
不錯!很好,但是有沒有更明確的方式,而不是明確定義每個子屬性的約束? – 2013-04-24 15:29:07