grails fixtures和域类验证问题

时间:2013-06-27 16:14:01

标签: grails fixtures

我是grails中的新手,并且在我的代码中遇到了这个问题。我正在尝试使用fixture来构建以下域对象的实例:

class Tree {
    static constraints = {
        relationships(
            validator: { relationships, tree->
                relationships && 
                !(relationships.isEmpty()) && 
                relationships.every { it.validate() 
            }
        )
    }
    static hasMany = [
        relationships: Branch
    ]
}

class Branch {
    Tree tree
    static constraints = {}
}

我尝试过这些实现,所有这些都会导致验证错误:

fixture {
    Build {
        //oak(Tree)
        //oakBranch(Branch){ tree = oak }

        //oakBranch(Branch){ tree = ref("oak") }
        //oak(Tree)

        //oak(Tree){ relationships = [ ref("oakBranch") ] }
        //oakBranch(Branch)
    }
}

所有实现都返回相同的错误:“'关系'字段:拒绝值[null]”。任何帮助将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:1)

我不确定您要通过检查relationships && !(relationships?.isEmpty()) &&来尝试使用自定义验证器完成什么,但是,如果您将其删除并重新编写代码,则可以绕过错误:

class Tree {
    static constraints = {
        relationships (
                validator: { relationships, tree->
//                    relationships &&
//                            !(relationships?.isEmpty()) &&
                            relationships.every { it.validate()}
                }
        )
    }
    static hasMany = [relationships: Branch]
}

fixture.groovy:

import tree.*
fixture {
    Build {
        oak(Tree)  {  }
        oakBranch(Branch){ tree = oak }

        //oakBranch(Branch){ tree = ref("oak") }
        //oak(Tree)

        //oak(Tree){ relationships = [ ref("oakBranch") ] }
        //oakBranch(Branch)
    }
}