groovygrailsconstraintspersistent

How to check if a PersistenProperty is a constrained property in groovy?


I am new to grails, and I am having a problem on how to check if a persistent property is constrained in Grails 6 :

List<PersistentProperty> persistentProperties =  grailsApplication.mappingContext.getPersistentEntity("Entity").persistentProperties

List constrainedProperties  = persistentProperties.find { isContrainedProperty(it) }
 

Please help me in writing the method "isContrainedProperty(it)"

Thanks!

Expect : writing a method that checks if given PersistentProperty is contrained in Grails 6


Solution

  • I found that the constraints block is processed into a property called 'constrainedProperties' on the PersistentEntity objects javaClass.

    So in your example:

    PersistentEntity e = grailsApplication.mappingContext.getPersistentEntity('Entity')
    Map constraints = e.javaClass.constrainedProperties
    

    Gives you a map of all the constraints on the object where the keys are the names of the properties, the values are of type ConstrainedProperty http://gorm.grails.org/latest/api//grails/gorm/validation/ConstrainedProperty.html

    From here I expect you can find the information you are looking for.

    I realize this solution is going behind the scenes of Grails a little bit but I haven't found a better way to find out this information.