我正在使用 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
它总是返回,并且我在( ) 中的自定义验证器永远不会被调用。从 grails 文档来看,似乎支持这种嵌套结构(默认为 true)。但是,也许这条规则只适用于域对象而不适用于命令对象?还是我只是在这里遗漏了什么?true
SectionCommand
answerNotBlank
deepValidate