Author: mrdon
Date: Sat Feb 16 03:12:16 2008
New Revision: 628262

URL: http://svn.apache.org/viewvc?rev=628262&view=rev
Log:
Adding support for short circut validations in javascript
WW-2270

Modified:
    
struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close-validate.ftl
    
struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-2.txt
    
struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-22.txt

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=628262&r1=628261&r2=628262&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
 Sat Feb 16 03:12:16 2008
@@ -41,6 +41,7 @@
         clearErrorLabels(form);
 
         var errors = false;
+        var continueValidation = true;
     <#list parameters.tagNames as tagName>
         <#list tag.getValidators("${tagName}") as validator>
         // field name: ${validator.fieldName}
@@ -52,14 +53,16 @@
             if (field.value == "") {
                 addError(field, error);
                 errors = true;
+                <#if validator.shortCircuit>continueValidation = false;</#if>
             }
             <#elseif validator.validatorType = "requiredstring">
-            if (field.value != null && (field.value == "" || 
field.value.replace(/^\s+|\s+$/g,"").length == 0)) {
+            if (continueValidation && field.value != null && (field.value == 
"" || field.value.replace(/^\s+|\s+$/g,"").length == 0)) {
                 addError(field, error);
                 errors = true;
+                <#if validator.shortCircuit>continueValidation = false;</#if>
             }
             <#elseif validator.validatorType = "stringlength">
-            if (field.value != null) {
+            if (continueValidation && field.value != null) {
                 var value = field.value;
                 <#if validator.trim>
                     //trim field value
@@ -72,35 +75,40 @@
                     (${validator.maxLength?c} > -1 && value.length > 
${validator.maxLength?c})) {
                     addError(field, error);
                     errors = true;
+                    <#if validator.shortCircuit>continueValidation = 
false;</#if>
                 }
             }
             <#elseif validator.validatorType = "regex">
-            if (field.value != null && 
!field.value.match("${validator.expression?js_string}")) {
+            if (continueValidation && field.value != null && 
!field.value.match("${validator.expression?js_string}")) {
                 addError(field, error);
                 errors = true;
+                <#if validator.shortCircuit>continueValidation = false;</#if>
             }
             <#elseif validator.validatorType = "email">
-            if (field.value != null && field.value.length > 0 && 
field.value.match(/\b(^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\.[A-Za-z0-9-]+)*((\.[A-Za-z0-9]{2,})|(\.[A-Za-z0-9]{2,}\.[A-Za-z0-9]{2,}))$)\b/gi)==null)
 {
+            if (continueValidation && field.value != null && 
field.value.length > 0 && 
field.value.match(/\b(^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\.[A-Za-z0-9-]+)*((\.[A-Za-z0-9]{2,})|(\.[A-Za-z0-9]{2,}\.[A-Za-z0-9]{2,}))$)\b/gi)==null)
 {
                 addError(field, error);
                 errors = true;
+                <#if validator.shortCircuit>continueValidation = false;</#if>
             }
             <#elseif validator.validatorType = "url">
-            if (field.value != null && field.value.length > 0 && 
field.value.match(/(^(ftp|http|https):\/\/(\.[_A-Za-z0-9-]+)*(@?([A-Za-z0-9-])+)?(\.[A-Za-z0-9-]+)*((\.[A-Za-z0-9]{2,})|(\.[A-Za-z0-9]{2,}\.[A-Za-z0-9]{2,}))(:[0-9]+)?([/A-Za-z0-9?#_-]*)?$)/gi)==null)
 { 
+            if (continueValidation && field.value != null && 
field.value.length > 0 && 
field.value.match(/(^(ftp|http|https):\/\/(\.[_A-Za-z0-9-]+)*(@?([A-Za-z0-9-])+)?(\.[A-Za-z0-9-]+)*((\.[A-Za-z0-9]{2,})|(\.[A-Za-z0-9]{2,}\.[A-Za-z0-9]{2,}))(:[0-9]+)?([/A-Za-z0-9?#_-]*)?$)/gi)==null)
 {
                 addError(field, error);
                 errors = true;
+                <#if validator.shortCircuit>continueValidation = false;</#if>
             }
             <#elseif validator.validatorType = "int">
-            if (field.value != null) {
+            if (continueValidation && field.value != null) {
                 if (<#if validator.min?exists>parseInt(field.value) <
                      ${validator.min?c}<#else>false</#if> ||
                         <#if validator.max?exists>parseInt(field.value) >
                            ${validator.max?c}<#else>false</#if>) {
                     addError(field, error);
                     errors = true;
+                    <#if validator.shortCircuit>continueValidation = 
false;</#if>
                 }
             }
             <#elseif validator.validatorType = "double">
-            if (field.value != null) {
+            if (continueValidation && field.value != null) {
                 var value = parseFloat(field.value);
                 if (<#if validator.minInclusive?exists>value < 
${validator.minInclusive?c}<#else>false</#if> ||
                         <#if validator.maxInclusive?exists>value > 
${validator.maxInclusive?c}<#else>false</#if> ||
@@ -108,6 +116,7 @@
                         <#if validator.maxExclusive?exists>value >= 
${validator.maxExclusive?c}<#else>false</#if>) {
                     addError(field, error);
                     errors = true;
+                    <#if validator.shortCircuit>continueValidation = 
false;</#if>
                 }
             }
             </#if>

Modified: 
struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-2.txt
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-2.txt?rev=628262&r1=628261&r2=628262&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-2.txt
 (original)
+++ 
struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-2.txt
 Sat Feb 16 03:12:16 2008
@@ -35,6 +35,7 @@
         clearErrorMessages(form);
         clearErrorLabels(form);
         var errors = false;
+        var continueValidation=true;
         return !errors; }
 </script>
 

Modified: 
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=628262&r1=628261&r2=628262&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-22.txt
 (original)
+++ 
struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Formtag-22.txt
 Sat Feb 16 03:12:16 2008
@@ -35,12 +35,13 @@
         clearErrorMessages(form);
         clearErrorLabels(form);
         var errors = false;
+        var continueValidation = true;
          //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(continueValidation && field.value!=null){
                 if(parseInt(field.value)<6000||parseInt(field.value)>10000){
                     addError(field,error);
                     errors=true;


Reply via email to