我正在使用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