不可变字段的 Grails 验证

grails validation of immutable fields

我有 4 个需要价格的字段 (BigDecimal),但其中只有一个应该包含价格。如果 price2 包含 255.95 并且有人想要在 price1 中输入价格那么这应该被拒绝并显示一条消息说你必须清理另一个(在这种情况下是 price2) 我尝试在域中这样做,但它对我不起作用。

一个例子:

class Author {
    def String name
    def String email
    def BigDecimal price1
    def BigDecimal price2
    def BigDecimal price3
//    static hasMany = [books: Book]

    static constraints = {
        name nullable:true
        email nullable:true
        price1(nullable:true,
           validator: { val, obj -> 
              (obj.price2==null) and (obj.price3 == null) }) 
        price2(nullable:true, 
           validator: { val, obj -> 
              (obj.price1==null) and (obj.price3 == null) }) 
        price3(nullable:true, 
           validator: { val, obj -> 
              (obj.price1==null) and (obj.price2 == null) })   
    }
    static mapping = {
        name    column: "AuthorName", sqltype:"char", length:25
    }
    String toString() {
        return name
    }
    def beforeValidate() {
    }
}

我收到这个错误:

groovy.lang.MissingMethodException: No signature of method: masterdetail.Author.and() is applicable for argument types: (java.lang.Boolean) values: [true]
Possible solutions: any(), any(groovy.lang.Closure), find(), find(groovy.lang.Closure), find(java.lang.String), find(masterdetail.Author)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:81)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
    at masterdetail.Author$__clinit__closure1$_closure3.doCall(Author.groovy:16)

那么,怎么办?

最后我在 doelleri 的帮助下解决了这个问题。 我编辑了问题,使其包含最终解决方案。