문제

I added a custom validator in domain class for a property. But whenever I run the unit test and run validate() method I get an error message that the property could not be recognized in the class. when I remove my custom validator everything is working properly.

Thanks your your help!

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}"
    }
}
도움이 되었습니까?

해결책

Accessing the objects properties in a custom validator is a little bit different than just referencing the property. The validator closure takes one or two parameters, 1) the current property's value and 2) the object itself, if you need access to the rest of the object.

validator: { val -> val > 0 }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top