Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grails how to override g message tag

I am a newbie in Grails so you may find my question quite naive. I would like to override g message tag. I want to add new features in tag's behaviour and then call the original implementation from ValidationTagLib. As far as I am concerned I can create my own taglib where I redefine the tag. G message is a closure that actually calls the messageImpl method from ValidationTaglib. My question is how can I call this method? I tried this code to call the closure but instead of messages I have receive empty spaces:

class MyTagLib {

    static namespace = "g"

    def message = { attrs ->
        //my changes in tag's behaviour
        def validationTagLib = grailsAttributes.applicationContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib')
        validationTagLib.message.call(attrs)
    }
} 

I would very much appreciate your help!

like image 223
user2109125 Avatar asked Dec 03 '25 02:12

user2109125


1 Answers

You will need to extend the Grails ValidationTagLib

import org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib

class MyValidationTagLib extends ValidationTagLib {

    /**
     * Resolves a message code for a given error or code from the resource bundle.
     *
     * @emptyTag
     *
     * @attr error The error to resolve the message for. Used for built-in Grails messages.
     * @attr message The object to resolve the message for. Objects must implement org.springframework.context.MessageSourceResolvable.
     * @attr code The code to resolve the message for. Used for custom application messages.
     * @attr args A list of argument values to apply to the message, when code is used.
     * @attr default The default message to output if the error or code cannot be found in messages.properties.
     * @attr encodeAs The name of a codec to apply, i.e. HTML, JavaScript, URL etc
     * @attr locale override locale to use instead of the one detected
     */
    Closure message = { attrs ->
        //my changes in tag's behaviour
        ValidationTagLib validationTagLib = grailsAttributes.applicationContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib')
        validationTagLib.message.call(attrs)
    }
}
like image 72
Dror Bereznitsky Avatar answered Dec 05 '25 00:12

Dror Bereznitsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!