【发布时间】:2013-04-17 09:22:43
【问题描述】:
我正在使用 grails 2.2.1 并尝试验证嵌套的命令结构。这是我的命令对象的简化版本:
@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 对象?还是我只是在这里遗漏了什么?
【问题讨论】:
标签: grails grails-2.0 grails-validation command-objects