Grails 在列表上使用自定义验证器
我有一个命令对象,如下所示:Grails 在列表上使用自定义验证器,grails,customvalidator,Grails,Customvalidator,我有一个命令对象,如下所示: class TestCreationCommand { String title List testItems = [].withLazyDefault {new VocabQuestion()} static constraints = { title(blank:false,maxLength:150) testItems( validator: { ti ->
class TestCreationCommand {
String title
List testItems = [].withLazyDefault {new VocabQuestion()}
static constraints = {
title(blank:false,maxLength:150)
testItems( validator: { ti ->
ti.each {it.validate()}
} )
}
}
class VocabQuestion {
static constraints = {
question(blank:false,maxLength:150)
answer(blank:false,maxLength:40)
}
static belongsTo = [vocabularyTest: VocabularyTest]
String question
String answer
}
测试项是VocabQuestion对象的列表。词汇问答如下:
class TestCreationCommand {
String title
List testItems = [].withLazyDefault {new VocabQuestion()}
static constraints = {
title(blank:false,maxLength:150)
testItems( validator: { ti ->
ti.each {it.validate()}
} )
}
}
class VocabQuestion {
static constraints = {
question(blank:false,maxLength:150)
answer(blank:false,maxLength:40)
}
static belongsTo = [vocabularyTest: VocabularyTest]
String question
String answer
}
我试图使用自定义的VALDATOR(在上面的命令类的约束中)验证VocabQuestion上的约束,但我一直收到以下错误消息
Return value from validation closure [validator] of property [testItems] of class [class vocabularytest.TestCreationCommand] is returning a list but the first element must be a string containing the error message code
我在这方面做了很多不同的尝试
我不确定消息告诉了我什么,也不知道如何调试闭包的返回值
有人能给我一些建议吗?你没有返回Grails理解的东西。您不能期望只返回任何东西,就让Grails知道如何处理它。返回布尔值或错误字符串
static constraints = {
title(blank:false,maxLength:150)
testItems( validator: { ti ->
Boolean errorExists = false
ti.each {
if (!it.validate()) {
errorExists = true
}
}
errorExists
})
}
看看这个,它可能是您需要的验证器的格式
.every将返回布尔值。是否尝试返回布尔值或错误消息键?在这里看到一个仍然适用于这两个的链接-没有运气。。。。。我快要放弃圣杯了。除非你坚持一个相当简单的CRUD方案,就像它从盒子里产生的那样,否则我发现我大部分时间都花在框架上,而不是帮助我提高生产力+1。如果有任何错误,我宁愿使用
for
循环和中断
,并将标志设置为true
。如果为真,则立即返回该标志。