Skip to content

support form html5's required attribute #112

Closed
wants to merge 1 commit into from
View
20 ...lugin-gsp/src/main/groovy/org/codehaus/groovy/grails/plugins/web/taglib/FormTagLib.groovy
@@ -117,6 +117,7 @@ class FormTagLib {
def fieldImpl(out, attrs) {
resolveAttributes(attrs)
+ handleRequiredAttribute(attrs)
attrs.id = attrs.id ?: attrs.name
out << "<input type=\"${attrs.remove('type')}\" "
outputAttributes(attrs, out)
@@ -155,6 +156,8 @@ class FormTagLib {
if (checked instanceof String) checked = Boolean.valueOf(checked)
+ handleRequiredAttribute(attrs)
+
if (value == null) value = false
out << "<input type=\"hidden\" name=\"_${name}\" /><input type=\"checkbox\" name=\"${name}\" "
@@ -233,6 +236,18 @@ class FormTagLib {
}
/**
+ * handle HTML's 'required' attribute: only add it if it's value is true in HTML even 'required="false"' marks the field as required
+ * @param attrs
+ */
+ void handleRequiredAttribute(attrs) {
+ def requiredString = attrs.remove('required')
+ Boolean required = Boolean.valueOf(requiredString)
+ if (required) {
+ attrs.required = true
+ }
+ }
+
+ /**
* Dump out attributes in HTML compliant fashion.
*/
void outputAttributes(attrs, writer) {
@@ -759,6 +774,7 @@ class FormTagLib {
if (disabled && Boolean.valueOf(disabled)) {
attrs.disabled = 'disabled'
}
+ handleRequiredAttribute(attrs)
writer << "<select name=\"${attrs.remove('name')?.encodeAsHTML()}\" "
// process remaining attributes
@@ -900,6 +916,8 @@ class FormTagLib {
attrs.disabled = 'disabled'
}
def checked = attrs.remove('checked') ? true : false
+ handleRequiredAttribute(attrs)
+
out << "<input type=\"radio\" name=\"${name}\"${ checked ? ' checked="checked" ' : ' '}value=\"${value?.toString()?.encodeAsHTML()}\" "
// process remaining attributes
outputAttributes(attrs, out)
@@ -921,6 +939,8 @@ class FormTagLib {
def values = attrs.remove('values')
def labels = attrs.remove('labels')
def name = attrs.remove('name')
+ handleRequiredAttribute(attrs)
+
values.eachWithIndex {val, idx ->
def it = new Expando()
it.radio = new StringBuilder("<input type=\"radio\" name=\"${name}\" ")
View
8 ...st-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/taglib/FormTagLibTests.groovy
@@ -61,6 +61,14 @@ class FormTagLibTests extends AbstractGrailsTagTests {
assertOutputEquals('<input type="text" name="testField" value="foo &gt; &quot; &amp; &lt; \'" id="testField" />', template, [value:/foo > " & < '/])
}
+ void testTextFieldTagWithRequired() {
+ def template = '<g:textField name="testField" value="${value}" required="false"/>'
+ assertOutputEquals('<input type="text" name="testField" value="foo &gt; &quot; &amp; &lt; \'" id="testField" />', template, [value:/foo > " & < '/])
+
+ template = '<g:textField name="testField" value="${value}" required="true"/>'
+ assertOutputEquals('<input type="text" name="testField" value="foo &gt; &quot; &amp; &lt; \'" id="testField" required="true" />', template, [value:/foo > " & < '/])
+ }
+
void testTextAreaWithBody() {
def template = '<g:textArea name="test">This is content</g:textArea>'
assertOutputEquals '<textarea name="test" id="test" >This is content</textarea>', template
Something went wrong with that request. Please try again.