From df1183e399842004d93334e2cd9d2c55b71e897a Mon Sep 17 00:00:00 2001 From: "Donald J. Brown" Date: Fri, 25 Jan 2008 14:27:51 +0000 Subject: [PATCH] Ensuring numbers are formatted in the client-side javascript validation in such a way to not break the code WW-1937 git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@615230 13f79535-47bb-0310-9956-ffa450edef68 --- .../template/xhtml/form-close-validate.ftl | 12 +- .../struts2/views/jsp/ui/FormTagTest.java | 219 ++++++++++++------ .../views/jsp/ui/IntValidationAction.java | 18 ++ .../struts2/views/jsp/ui/Formtag-22.txt | 52 +++++ .../jsp/ui/IntValidationAction-validation.xml | 10 + 5 files changed, 232 insertions(+), 79 deletions(-) create mode 100644 core/src/test/java/org/apache/struts2/views/jsp/ui/IntValidationAction.java create mode 100644 core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-22.txt create mode 100644 core/src/test/resources/org/apache/struts2/views/jsp/ui/IntValidationAction-validation.xml diff --git a/core/src/main/resources/template/xhtml/form-close-validate.ftl b/core/src/main/resources/template/xhtml/form-close-validate.ftl index d7a58e19a..adc690fca 100644 --- a/core/src/main/resources/template/xhtml/form-close-validate.ftl +++ b/core/src/main/resources/template/xhtml/form-close-validate.ftl @@ -92,9 +92,9 @@ END SNIPPET: supported-validators <#elseif validator.validatorType = "int"> if (field.value != null) { if (<#if validator.min?exists>parseInt(field.value) < - ${validator.min?string}<#else>false || + ${validator.min?c}<#else>false || <#if validator.max?exists>parseInt(field.value) > - ${validator.max?string}<#else>false) { + ${validator.max?c}<#else>false) { addError(field, error); errors = true; } @@ -102,10 +102,10 @@ END SNIPPET: supported-validators <#elseif validator.validatorType = "double"> if (field.value != null) { var value = parseFloat(field.value); - if (<#if validator.minInclusive?exists>value < ${validator.minInclusive?string}<#else>false || - <#if validator.maxInclusive?exists>value > ${validator.maxInclusive?string}<#else>false || - <#if validator.minExclusive?exists>value <= ${validator.minExclusive?string}<#else>false || - <#if validator.maxExclusive?exists>value >= ${validator.maxExclusive?string}<#else>false) { + if (<#if validator.minInclusive?exists>value < ${validator.minInclusive?c}<#else>false || + <#if validator.maxInclusive?exists>value > ${validator.maxInclusive?c}<#else>false || + <#if validator.minExclusive?exists>value <= ${validator.minExclusive?c}<#else>false || + <#if validator.maxExclusive?exists>value >= ${validator.maxExclusive?c}<#else>false) { addError(field, error); errors = true; } diff --git a/core/src/test/java/org/apache/struts2/views/jsp/ui/FormTagTest.java b/core/src/test/java/org/apache/struts2/views/jsp/ui/FormTagTest.java index a74aed7ce..d5fcf3eb8 100644 --- a/core/src/test/java/org/apache/struts2/views/jsp/ui/FormTagTest.java +++ b/core/src/test/java/org/apache/struts2/views/jsp/ui/FormTagTest.java @@ -128,87 +128,79 @@ public class FormTagTest extends AbstractUITagTest { */ public void testFormWithCustomOnsubmitEnabledWithValidateEnabled1() throws Exception { - com.opensymphony.xwork2.config.Configuration originalConfiguration = configurationManager.getConfiguration(); - ObjectFactory originalObjectFactory = ObjectFactory.getObjectFactory(); + final Container cont = container; + // used to determined if the form action needs js validation + configurationManager.setConfiguration(new com.opensymphony.xwork2.config.impl.DefaultConfiguration() { + private DefaultConfiguration self = this; + public Container getContainer() { + return new Container() { + public T inject(Class implementation) {return null;} + public void removeScopeStrategy() {} + public void setScopeStrategy(Strategy scopeStrategy) {} + public T getInstance(Class type, String name) {return null;} + public T getInstance(Class type) {return null;} + public Set getInstanceNames(Class type) {return null;} - try { - final Container cont = container; - // used to determined if the form action needs js validation - configurationManager.setConfiguration(new com.opensymphony.xwork2.config.impl.DefaultConfiguration() { - private DefaultConfiguration self = this; - public Container getContainer() { - return new Container() { - public T inject(Class implementation) {return null;} - public void removeScopeStrategy() {} - public void setScopeStrategy(Strategy scopeStrategy) {} - public T getInstance(Class type, String name) {return null;} - public T getInstance(Class type) {return null;} - public Set getInstanceNames(Class type) {return null;} + public void inject(Object o) { + cont.inject(o); + if (o instanceof Form) { + ((Form)o).setConfiguration(self); + } + } + }; + } + public RuntimeConfiguration getRuntimeConfiguration() { + return new RuntimeConfiguration() { + public ActionConfig getActionConfig(String namespace, String name) { + ActionConfig actionConfig = new ActionConfig("", name, "") { + public List getInterceptors() { + List interceptors = new ArrayList(); - public void inject(Object o) { - cont.inject(o); - if (o instanceof Form) { - ((Form)o).setConfiguration(self); + ValidationInterceptor validationInterceptor = new ValidationInterceptor(); + validationInterceptor.setIncludeMethods("*"); + + InterceptorMapping interceptorMapping = new InterceptorMapping("validation", validationInterceptor); + interceptors.add(interceptorMapping); + + return interceptors; } - } - }; - } - public RuntimeConfiguration getRuntimeConfiguration() { - return new RuntimeConfiguration() { - public ActionConfig getActionConfig(String namespace, String name) { - ActionConfig actionConfig = new ActionConfig("", name, "") { - public List getInterceptors() { - List interceptors = new ArrayList(); + public String getClassName() { + return ActionSupport.class.getName(); + } + }; + return actionConfig; + } - ValidationInterceptor validationInterceptor = new ValidationInterceptor(); - validationInterceptor.setIncludeMethods("*"); + public Map getActionConfigs() { + return null; + } + }; + } + }); - InterceptorMapping interceptorMapping = new InterceptorMapping("validation", validationInterceptor); - interceptors.add(interceptorMapping); + FormTag tag = new FormTag(); + tag.setPageContext(pageContext); + tag.setName("myForm"); + tag.setMethod("post"); + tag.setAction("myAction"); + tag.setAcceptcharset("UTF-8"); + tag.setEnctype("myEncType"); + tag.setTitle("mytitle"); + tag.setOnsubmit("submitMe()"); + tag.setValidate("true"); + tag.setNamespace(""); - return interceptors; - } - public String getClassName() { - return ActionSupport.class.getName(); - } - }; - return actionConfig; - } + UpDownSelectTag t = new UpDownSelectTag(); + t.setPageContext(pageContext); + t.setName("myUpDownSelectTag"); + t.setList("{}"); - public Map getActionConfigs() { - return null; - } - }; - } - }); + tag.doStartTag(); + t.doStartTag(); + t.doEndTag(); + tag.doEndTag(); - FormTag tag = new FormTag(); - tag.setPageContext(pageContext); - tag.setName("myForm"); - tag.setMethod("post"); - tag.setAction("myAction"); - tag.setAcceptcharset("UTF-8"); - tag.setEnctype("myEncType"); - tag.setTitle("mytitle"); - tag.setOnsubmit("submitMe()"); - tag.setValidate("true"); - tag.setNamespace(""); - - UpDownSelectTag t = new UpDownSelectTag(); - t.setPageContext(pageContext); - t.setName("myUpDownSelectTag"); - t.setList("{}"); - - tag.doStartTag(); - t.doStartTag(); - t.doEndTag(); - tag.doEndTag(); - - verify(FormTag.class.getResource("Formtag-2.txt")); - } - finally { - configurationManager.setConfiguration(originalConfiguration); - } + verify(FormTag.class.getResource("Formtag-2.txt")); } @@ -302,6 +294,87 @@ public class FormTagTest extends AbstractUITagTest { } } + /** + * Tests the numbers are formatted correctly to not break the javascript + */ + public void testFormWithCustomOnsubmitEnabledWithValidateEnabled3() throws Exception { + + final Container cont = container; + // used to determined if the form action needs js validation + configurationManager.setConfiguration(new com.opensymphony.xwork2.config.impl.DefaultConfiguration() { + private DefaultConfiguration self = this; + public Container getContainer() { + return new Container() { + public T inject(Class implementation) {return null;} + public void removeScopeStrategy() {} + public void setScopeStrategy(Strategy scopeStrategy) {} + public T getInstance(Class type, String name) {return null;} + public T getInstance(Class type) {return null;} + public Set getInstanceNames(Class type) {return null;} + + public void inject(Object o) { + cont.inject(o); + if (o instanceof Form) { + ((Form)o).setConfiguration(self); + } + } + }; + } + public RuntimeConfiguration getRuntimeConfiguration() { + return new RuntimeConfiguration() { + public ActionConfig getActionConfig(String namespace, String name) { + ActionConfig actionConfig = new ActionConfig("", name, IntValidationAction.class.getName()) { + public List getInterceptors() { + List interceptors = new ArrayList(); + + ValidationInterceptor validationInterceptor = new ValidationInterceptor(); + validationInterceptor.setIncludeMethods("*"); + + InterceptorMapping interceptorMapping = new InterceptorMapping("validation", validationInterceptor); + interceptors.add(interceptorMapping); + + return interceptors; + } + public String getClassName() { + return IntValidationAction.class.getName(); + } + }; + return actionConfig; + } + + public Map getActionConfigs() { + return null; + } + }; + } + }); + + FormTag tag = new FormTag(); + tag.setPageContext(pageContext); + tag.setName("myForm"); + tag.setMethod("post"); + tag.setAction("myAction"); + tag.setAcceptcharset("UTF-8"); + tag.setEnctype("myEncType"); + tag.setTitle("mytitle"); + tag.setOnsubmit("submitMe()"); + tag.setValidate("true"); + tag.setNamespace(""); + + UpDownSelectTag t = new UpDownSelectTag(); + t.setPageContext(pageContext); + t.setName("myUpDownSelectTag"); + t.setList("{}"); + + tag.doStartTag(); + tag.getComponent().getParameters().put("actionClass", IntValidationAction.class); + t.doStartTag(); + t.doEndTag(); + tag.doEndTag(); + + verify(FormTag.class.getResource("Formtag-22.txt")); + } + /** * This test with form tag validation disabled. */ diff --git a/core/src/test/java/org/apache/struts2/views/jsp/ui/IntValidationAction.java b/core/src/test/java/org/apache/struts2/views/jsp/ui/IntValidationAction.java new file mode 100644 index 000000000..79139c1a1 --- /dev/null +++ b/core/src/test/java/org/apache/struts2/views/jsp/ui/IntValidationAction.java @@ -0,0 +1,18 @@ +package org.apache.struts2.views.jsp.ui; + +import com.opensymphony.xwork2.ActionSupport; + +/** + * + */ +public class IntValidationAction extends ActionSupport { + private int longint; + + public int getLongint() { + return longint; + } + + public void setLongint(int longint) { + this.longint = longint; + } +} diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-22.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-22.txt new file mode 100644 index 000000000..b2aa0efa8 --- /dev/null +++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-22.txt @@ -0,0 +1,52 @@ + + +
+ + + + +
+ + + +
+
+    +    +    +
+
+
+ + + + + + diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/IntValidationAction-validation.xml b/core/src/test/resources/org/apache/struts2/views/jsp/ui/IntValidationAction-validation.xml new file mode 100644 index 000000000..051b0fb5c --- /dev/null +++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/IntValidationAction-validation.xml @@ -0,0 +1,10 @@ + + + + + 6000 + 10000 + bar must be between ${min} and ${max}. + + +