Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ppazos/7d3a92e9dfa69d192fadf741175d8a4a to your computer and use it in GitHub Desktop.
Save ppazos/7d3a92e9dfa69d192fadf741175d8a4a to your computer and use it in GitHub Desktop.
Extension of Grails ValidationTagLib
package com.hussain.pf
import groovy.xml.MarkupBuilder
import org.apache.commons.lang.StringEscapeUtils
import org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib
class CustomValidationTagLib extends ValidationTagLib {
/**
* Loops through each error and renders it using one of the supported mechanisms (defaults to "list" if unsupported).
*
* @attr bean REQUIRED The bean to check for errors
* @attr field The field of the bean or model reference to check
* @attr model The model reference to check for errors
*/
def renderErrors = { attrs, body ->
def renderAs = attrs.remove('as')
if (!renderAs) renderAs = 'list'
if (renderAs == 'list') {
def codec = attrs.codec ?: 'HTML'
if (codec == 'none') codec = ''
out << "<ul>"
out << eachErrorInternal(attrs, {
out << "<li>${message(error:it, encodeAs:codec)}</li>"
})
out << "</ul>"
}
else if (renderAs.equalsIgnoreCase("xml")) {
def mkp = new MarkupBuilder(out)
mkp.errors() {
eachErrorInternal(attrs, {
error(object: it.objectName,
field: it.field,
message: message(error:it)?.toString(),
'rejected-value': StringEscapeUtils.escapeXml(it.rejectedValue))
})
}
}
else if (renderAs) {
def codec = attrs.codec ?: 'HTML'
if (codec == 'none') codec = ''
out << eachErrorInternal(attrs, {
out << "<${renderAs}>${message(error:it, encodeAs:codec)}</${renderAs}>"
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment