From 4b77bc27b06732ff27d33c987bbc0e8ea3752934 Mon Sep 17 00:00:00 2001 From: Musachy Barroso Date: Fri, 27 Mar 2009 14:49:39 +0000 Subject: [PATCH] WW-3054 Broken FreeMarker template for double validation. Test added. git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@759162 13f79535-47bb-0310-9956-ffa450edef68 --- .../template/xhtml/form-close-validate.ftl | 8 +- .../views/jsp/ui/DoubleValidationAction.java | 39 +++++++++ .../struts2/views/jsp/ui/FormTagTest.java | 83 ++++++++++++++++++- .../ui/DoubleValidationAction-validation.xml | 10 +++ .../struts2/views/jsp/ui/Formtag-24.txt | 54 ++++++++++++ 5 files changed, 189 insertions(+), 5 deletions(-) create mode 100644 core/src/test/java/org/apache/struts2/views/jsp/ui/DoubleValidationAction.java create mode 100644 core/src/test/resources/org/apache/struts2/views/jsp/ui/DoubleValidationAction-validation.xml create mode 100644 core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-24.txt 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 b9521058c..8a780c44d 100644 --- a/core/src/main/resources/template/xhtml/form-close-validate.ftl +++ b/core/src/main/resources/template/xhtml/form-close-validate.ftl @@ -110,10 +110,10 @@ END SNIPPET: supported-validators <#elseif validator.validatorType = "double"> if (continueValidation && field.value != null) { var value = parseFloat(field.value); - if (<#if validator.minInclusive??>value < ${validator.minInclusive?c}<#else>false || - <#if validator.maxInclusive??>value > ${validator.maxInclusive?c}<#else>false || - <#if validator.minExclusive??>value <= ${validator.minExclusive?c}<#else>false || - <#if validator.maxExclusive??>value >= ${validator.maxExclusive?c}<#else>false) { + if (<#if validator.minInclusive??>value < ${validator.minInclusive}<#else>false || + <#if validator.maxInclusive??>value > ${validator.maxInclusive}<#else>false || + <#if validator.minExclusive??>value <= ${validator.minExclusive}<#else>false || + <#if validator.maxExclusive??>value >= ${validator.maxExclusive}<#else>false) { addError(field, error); errors = true; <#if validator.shortCircuit>continueValidation = false; diff --git a/core/src/test/java/org/apache/struts2/views/jsp/ui/DoubleValidationAction.java b/core/src/test/java/org/apache/struts2/views/jsp/ui/DoubleValidationAction.java new file mode 100644 index 000000000..622751f8d --- /dev/null +++ b/core/src/test/java/org/apache/struts2/views/jsp/ui/DoubleValidationAction.java @@ -0,0 +1,39 @@ +/* + * $Id$ + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.struts2.views.jsp.ui; + +import com.opensymphony.xwork2.ActionSupport; + +/** + * + */ +public class DoubleValidationAction extends ActionSupport { + private double longint; + + public double getLongint() { + return longint; + } + + public void setLongint(double longint) { + this.longint = longint; + } +} 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 76aaa1d99..ad27a1622 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 @@ -31,7 +31,6 @@ import org.apache.struts2.StrutsConstants; import org.apache.struts2.TestAction; import org.apache.struts2.TestConfigurationProvider; import org.apache.struts2.components.Form; -import org.apache.struts2.dispatcher.mapper.DefaultActionMapper; import org.apache.struts2.views.jsp.AbstractUITagTest; import org.apache.struts2.views.jsp.ActionTag; @@ -392,6 +391,88 @@ public class FormTagTest extends AbstractUITagTest { verify(FormTag.class.getResource("Formtag-22.txt")); } +/** + * Tests the numbers are formatted correctly to not break the javascript, using doubles + */ + public void testFormWithCustomOnsubmitEnabledWithValidateEnabled4() 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, DoubleValidationAction.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 DoubleValidationAction.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", DoubleValidationAction.class); + t.doStartTag(); + t.doEndTag(); + tag.doEndTag(); + + verify(FormTag.class.getResource("Formtag-24.txt")); + } + + /** * This test with form tag validation disabled. */ diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/DoubleValidationAction-validation.xml b/core/src/test/resources/org/apache/struts2/views/jsp/ui/DoubleValidationAction-validation.xml new file mode 100644 index 000000000..7a3c865d4 --- /dev/null +++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/DoubleValidationAction-validation.xml @@ -0,0 +1,10 @@ + + + + + 6000.10 + 10000.10 + bar must be between ${minInclusive} and ${maxInclusive}. + + + \ No newline at end of file diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-24.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-24.txt new file mode 100644 index 000000000..7fce59621 --- /dev/null +++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-24.txt @@ -0,0 +1,54 @@ + + +
+ + + + +
+ + + +
+
+    +    +    +
+
+
+ + + + + +