I'm creating a mixin to add to my controllers that has some basic utility functions. One of the functions I'd like to perform in the mixin is resolving errors to their string using the g.message
taglib. Unfortunately, I can't seem to access the the GrailsApplication
within my static mixin method.
How would I access the Grails taglibs from my mixin, or is there a better way to achieve what it is I'm doing -- sharing common code between all controllers?
Here's the code I'm running. I think the issue is how to access Grails tag libs in static methods:
static def preparePostResponse(domainInstance) {
def grailsApplication = new User().domainClass.grailsApplication
def postResponse = new AjaxPostResponse(domainObject: domainInstance)
if (domainInstance.hasErrors()) {
g.eachError(bean: domainInstance) {
postResponse.errors."${it.field}" = g.message(error: it)
}
postResponse.success = false
postResponse.message = "There was an error"
}
else {
postResponse.success = true
postResponse.message = "Success"
}
return postResponse
}