Created
October 6, 2011 16:41
-
-
Save hussainanjar/1267898 to your computer and use it in GitHub Desktop.
Extension of Grails ValidationTagLib
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 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