Author: mrdon Date: Fri Jan 25 06:27:51 2008 New Revision: 615230 URL: http://svn.apache.org/viewvc?rev=615230&view=rev Log: Ensuring numbers are formatted in the client-side javascript validation in such a way to not break the code WW-1937
Added: struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/IntValidationAction.java struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-22.txt struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/IntValidationAction-validation.xml Modified: struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close-validate.ftl struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/FormTagTest.java Modified: struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close-validate.ftl URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close-validate.ftl?rev=615230&r1=615229&r2=615230&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close-validate.ftl (original) +++ struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close-validate.ftl Fri Jan 25 06:27:51 2008 @@ -92,9 +92,9 @@ <#elseif validator.validatorType = "int"> if (field.value != null) { if (<#if validator.min?exists>parseInt(field.value) < - ${validator.min?string}<#else>false</#if> || + ${validator.min?c}<#else>false</#if> || <#if validator.max?exists>parseInt(field.value) > - ${validator.max?string}<#else>false</#if>) { + ${validator.max?c}<#else>false</#if>) { addError(field, error); errors = true; } @@ -102,10 +102,10 @@ <#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> || - <#if validator.maxInclusive?exists>value > ${validator.maxInclusive?string}<#else>false</#if> || - <#if validator.minExclusive?exists>value <= ${validator.minExclusive?string}<#else>false</#if> || - <#if validator.maxExclusive?exists>value >= ${validator.maxExclusive?string}<#else>false</#if>) { + if (<#if validator.minInclusive?exists>value < ${validator.minInclusive?c}<#else>false</#if> || + <#if validator.maxInclusive?exists>value > ${validator.maxInclusive?c}<#else>false</#if> || + <#if validator.minExclusive?exists>value <= ${validator.minExclusive?c}<#else>false</#if> || + <#if validator.maxExclusive?exists>value >= ${validator.maxExclusive?c}<#else>false</#if>) { addError(field, error); errors = true; } Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/FormTagTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/FormTagTest.java?rev=615230&r1=615229&r2=615230&view=diff ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/FormTagTest.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/FormTagTest.java Fri Jan 25 06:27:51 2008 @@ -128,87 +128,79 @@ */ public void testFormWithCustomOnsubmitEnabledWithValidateEnabled1() throws Exception { - com.opensymphony.xwork2.config.Configuration originalConfiguration = configurationManager.getConfiguration(); - ObjectFactory originalObjectFactory = ObjectFactory.getObjectFactory(); - - 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> T inject(Class<T> implementation) {return null;} - public void removeScopeStrategy() {} - public void setScopeStrategy(Strategy scopeStrategy) {} - public <T> T getInstance(Class<T> type, String name) {return null;} - public <T> T getInstance(Class<T> type) {return null;} - public Set<String> 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(); - - ValidationInterceptor validationInterceptor = new ValidationInterceptor(); - validationInterceptor.setIncludeMethods("*"); - - InterceptorMapping interceptorMapping = new InterceptorMapping("validation", validationInterceptor); - interceptors.add(interceptorMapping); - - return interceptors; - } - public String getClassName() { - return ActionSupport.class.getName(); - } - }; - return actionConfig; - } - - public Map getActionConfigs() { - return null; - } - }; - } - }); + 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> T inject(Class<T> implementation) {return null;} + public void removeScopeStrategy() {} + public void setScopeStrategy(Strategy scopeStrategy) {} + public <T> T getInstance(Class<T> type, String name) {return null;} + public <T> T getInstance(Class<T> type) {return null;} + public Set<String> 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(); - 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(""); + ValidationInterceptor validationInterceptor = new ValidationInterceptor(); + validationInterceptor.setIncludeMethods("*"); - UpDownSelectTag t = new UpDownSelectTag(); - t.setPageContext(pageContext); - t.setName("myUpDownSelectTag"); - t.setList("{}"); + InterceptorMapping interceptorMapping = new InterceptorMapping("validation", validationInterceptor); + interceptors.add(interceptorMapping); - tag.doStartTag(); - t.doStartTag(); - t.doEndTag(); - tag.doEndTag(); + return interceptors; + } + public String getClassName() { + return ActionSupport.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(); + t.doStartTag(); + t.doEndTag(); + tag.doEndTag(); - verify(FormTag.class.getResource("Formtag-2.txt")); - } - finally { - configurationManager.setConfiguration(originalConfiguration); - } + verify(FormTag.class.getResource("Formtag-2.txt")); } @@ -300,6 +292,87 @@ finally { configurationManager.setConfiguration(originalConfiguration); } + } + + /** + * 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> T inject(Class<T> implementation) {return null;} + public void removeScopeStrategy() {} + public void setScopeStrategy(Strategy scopeStrategy) {} + public <T> T getInstance(Class<T> type, String name) {return null;} + public <T> T getInstance(Class<T> type) {return null;} + public Set<String> 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")); } /** Added: struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/IntValidationAction.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/IntValidationAction.java?rev=615230&view=auto ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/IntValidationAction.java (added) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/IntValidationAction.java Fri Jan 25 06:27:51 2008 @@ -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; + } +} Added: struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-22.txt URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-22.txt?rev=615230&view=auto ============================================================================== --- struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-22.txt (added) +++ struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-22.txt Fri Jan 25 06:27:51 2008 @@ -0,0 +1,52 @@ +<script type="text/javascript" src="/struts/xhtml/validation.js"></script> +<script type="text/javascript "src="/struts/utils.js"></script> +<form id="myAction" name="myForm" onsubmit="submitMe(); return validateForm_myAction();" action="/myAction.action" method="post" enctype="myEncType" title="mytitle" accept-charset="UTF-8"> +<table class="wwFormTable"> <tr> + <td class="tdLabel"></td> + <td> <script type="text/javascript" src="/struts/optiontransferselect.js"></script> +<table> +<tr><td> +<select name="myUpDownSelectTag" size="5" id="myAction_myUpDownSelectTag" multiple="multiple"> +</select></td></tr> +<tr><td> + <input type="button" value="^" onclick="moveOptionUp(document.getElementById('myAction_myUpDownSelectTag'), 'key', '');" /> + <input type="button" value="v" onclick="moveOptionDown(document.getElementById('myAction_myUpDownSelectTag'), 'key', '');" /> + <input type="button" value="*" onclick="selectAllOptions(document.getElementById('myAction_myUpDownSelectTag'), 'key', '');" /> +</td></tr> +</table> +</td> +</tr> +</table> +</form> + + +<script type="text/javascript"> + var containingForm = document.getElementById("myAction"); + StrutsUtils.addEventListener(containingForm, "submit", + function(evt) { + var updownselectObj = document.getElementById("myAction_myUpDownSelectTag"); + selectAllOptionsExceptSome(updownselectObj, "key", ""); + }, true); +</script> + +<script type="text/javascript"> + function validateForm_myAction() { + form = document.getElementById("myAction"); + clearErrorMessages(form); + clearErrorLabels(form); + var errors = false; + //fieldname:myUpDownSelectTag + //validatorname:int + if(form.elements['myUpDownSelectTag']){ + field=form.elements['myUpDownSelectTag']; + var error="bar must be between 6000 and 10000."; + if(field.value!=null){ + if(parseInt(field.value)<6000||parseInt(field.value)>10000){ + addError(field,error); + errors=true; + } + } + } + return!errors; + }</script> + Added: struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/IntValidationAction-validation.xml URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/IntValidationAction-validation.xml?rev=615230&view=auto ============================================================================== --- struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/IntValidationAction-validation.xml (added) +++ struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/IntValidationAction-validation.xml Fri Jan 25 06:27:51 2008 @@ -0,0 +1,10 @@ +<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd"> +<validators> + <field name="myUpDownSelectTag"> + <field-validator type="int"> + <param name="min">6000</param> + <param name="max">10000</param> + <message>bar must be between ${min} and ${max}.</message> + </field-validator> + </field> +</validators>