grailsgrails-ormgspgrails-constraints

Grails 3.3.2 accessing custom meta constraints


In a grails 2.4.4 project, I was able to define my own custom constraint (called 'supportsToUrl') on a domain property and use it as a tag to control rendering logic in my GSP.

GSP rendering code:

if(domainClass.constraints[p.name].getMetaConstraintValue('supportsToUrl'))

Domain class constraint:

static constraints = {
    embedCode(nullable:true, blank:true, unique:false, display:true, supportsToUrl:true)
}

In Upgrading from Grails 3.2.x in section "Grails Validator and ConstrainedProperty API Deprecated" there is discussion about how this functionality has been moved. However, I did not see anything in the new API that refers to meta constraints.

My question is: How do I access custom constraints in Grails 3.3.2?


Solution

  • You could access meta constraints still in Grails 3.3.* From Validateable trait getConstraintsMap().

    Example list of all properties which supports url (supportsToUrl: true)

    Set<String> supportsUrlProperties = new HashSet<>()
    Map<String, Constrained> constraints = domainObject.getConstraintsMap()
    if(constraints) {
         constraints.values().each { Constrained constrained ->
            DefaultConstrainedProperty propertyToCheck = constrained.properties?.property as DefaultConstrainedProperty
            if(propertyToCheck) {
                def supportsToUrlConstraint = propertyToCheck.getMetaConstraintValue('supportsToUrl')
                if (supportsToUrlConstraint != null && BooleanUtils.isTrue(supportsToUrlConstraint as Boolean)) {
                    supportsUrlProperties.add(propertyToCheck.getPropertyName())
                }
            }
        }
    }
    

    Be aware, that it sees only constraints from domain/entity (abstract or not) which are marked with this Validateable trait. Class hierarchy won't apply - when root/super class implements it, then top class's constraints still are not visible, until you mark it also as Validateable.