【问题标题】:Grails Domain ValidatorGrails 域验证器
【发布时间】:2012-10-09 20:35:56
【问题描述】:

我在域类中为属性添加了一个自定义验证器。但是,每当我运行单元测试并运行 validate() 方法时,我都会收到一条错误消息,指出在类中无法识别该属性。当我删除我的自定义验证器时,一切正常。

感谢您的帮助!

class Region {
int identifier
    BigDecimal leftUpLatitude
    BigDecimal leftUpLongitude
    BigDecimal rigthDownLatitude
    BigDecimal rigthDownLongitude

    static constraints = {  
        identifier unique:true, validator: {return identifier > 0} 
        leftUpLatitude min:-90.00, max:90.00
        leftUpLongitude min:-180.00, max:180.00
        rigthDownLatitude min:-90.00, max:90.00
        rigthDownLongitude min:-180.00, max:180.00   
    }

    boolean isValidRegion(){
        if ((leftUpLatitude > rigthDownLatitude) && ( leftUpLongitude < rigthDownLongitude))
            return true
        else
        return false    
    }
    String toString(){
        return "${identifier}"
    }
}

【问题讨论】:

  • 您介意发布包含验证器的域类吗?

标签: grails grails-orm


【解决方案1】:

在自定义验证器中访问对象属性与仅引用属性有点不同。验证器闭包接受一两个参数,1) 当前属性的值和 2) 对象本身,如果您需要访问对象的其余部分。

validator: { val -> val > 0 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多