Last active
December 13, 2015 19:08
-
-
Save sergiomichels/4960135 to your computer and use it in GitHub Desktop.
Try to register custom constraints based on the type of the field. Uses the GrailsDomainClass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package br.com.insoft4.core.constraints | |
| import org.codehaus.groovy.grails.commons.DefaultGrailsDomainClassProperty | |
| import org.codehaus.groovy.grails.commons.GrailsDomainClass | |
| import org.codehaus.groovy.grails.validation.ConstrainedProperty; | |
| import br.com.insoft4.core.types.EmpresaIdentity; | |
| import br.com.insoft4.core.types.SimNao | |
| import grails.util.Holders | |
| class ConstraintRegistrar { | |
| static void registerConstraints() { | |
| List<GrailsDomainClass> domainClasses = Holders.getGrailsApplication().getArtefacts("Domain") | |
| domainClasses.each ConstraintRegistrar.®isterConstraints | |
| } | |
| static void registerConstraints(Class clazz) { | |
| GrailsDomainClass domainClass = Holders.getGrailsApplication().getDomainClass(clazz.name) | |
| registerConstraints(domainClass) | |
| } | |
| static void registerConstraints( GrailsDomainClass domainClass ) { | |
| registerConstraint(domainClass, SimNao.class, SimNaoConstraint.CONSTRAINT_NAME) | |
| registerConstraint(domainClass, EmpresaIdentity.class, EmpresaIdentityConstraint.CONSTRAINT_NAME) | |
| } | |
| static void registerConstraint(GrailsDomainClass domainClass, Class type, String consName) { | |
| List<DefaultGrailsDomainClassProperty> props = domainClass.properties.findAll { type.isAssignableFrom( it.type ) } | |
| def constraints = domainClass.constraints | |
| for(DefaultGrailsDomainClassProperty prop : props) { | |
| String propName = prop.name | |
| ConstrainedProperty constraint = (constraints."$propName") | |
| if( !constraint.getAppliedConstraint(consName) ) { | |
| constraint.applyConstraint(consName, true) | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment