0

I am new to fixtures in grails and have bumped into this problem in my code. I am trying to use fixtures to build an instance of the following domain objects:

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 = {}
}

I have tried these implementations, all of which result in a validation error:

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

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

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

All implementations return the same error: "on field of 'relationships': rejected value [null]". Any help will be much appreciated, thanks.

4

1 回答 1

1

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

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

夹具.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)
    }
}
于 2013-06-27T18:09:58.897 回答