Fork me on GitHub

LD.

Music, software, life… and stuff.

[ Twitter ] [ GitHub ] [ Linked In ]

Providing non-domain validateables in Grails plugins.

Grails now (1.2+) supports making arbitrary classes validateable. This is easy enough, but what if you have a plugin with a non-domain class that needs to be validateable?

What you might try is adding the necessary config into the Config.groovy in your plugin. This is not going to work though because that’s not going to be sourced when your plugin is deployed into an application.

So here’s a solution:

package grails.plugin.radness

class RadnessGrailsPlugin {
    def loadBefore = ['validation'] // important

    def doWithDynamicMethods = {
        if (!application.config.grails.validateable.classes) {
            application.config.grails.validateable.classes = []
        }
        application.config.grails.validateable.classes << RadnessValidateable
    }
}

The RadnessValidateable class gets configured with all the validation bits. You don’t need to annotate the class with org.codehaus.groovy.grails.validation.Validateable, the above is enough to make it work.

Posted: Feb 10th, 2010 @ 3:50 pm

Tags: #grails  #software  

Comments

Archive · RSS · Theme by Autumn