Question

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}"
    }
}
Was it helpful?

Solution

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 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top