Hi,

I got a patch that might be slightly related to 
https://issues.apache.org/jira/browse/VALIDATOR-116. It addresses the following 
two problems.

1.) If an indexed field is validated the validation terminates on the first 
index failing. My patch makes this configurable within the field element via an 
attribute "stopOnFail". If this is false just the validation for the current 
index is terminated if a dependency validation fails. If it is true (Default) 
there is no change in behaviour.

2.) In VALIDATOR-116 it is said that a major problem with indexed fields is 
that you can't get different error messages. My patch makes this possible by 
allowing a back-reference token "[]" (as used internally for the key of the 
field) to be used within msg keys, arg keys and var values. In case of an 
indexed property these are replaced by [0],[1],[2],... during the iteration 
over all the indexes of the validated field.

By the way, should I have (or should I still) opened an "Improvement" within 
Jira for this or has it been correct to first post this patch here?

Björn Eickvonder

Diese E-Mail enthaelt vertrauliche und/oder rechtlich geschuetzte Informationen.
Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtuemlich 
erhalten haben, 
informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. 
Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail bzw. Inhalte 
hieraus ist nicht gestattet.

Index: D:/workspace3.1/trunk/conf/share/validator_1_3_0.dtd
===================================================================
--- D:/workspace3.1/trunk/conf/share/validator_1_3_0.dtd        (revision 
475615)
+++ D:/workspace3.1/trunk/conf/share/validator_1_3_0.dtd        (working copy)
@@ -128,14 +128,18 @@
                      list and then loop through the list performing the
                      validations for this field.
 
+     stopOnFail      A boolean value that defines if all index values should
+                     be validated or validation should stop on the first
+                     index failing (Default: true)           
+
 -->
 <!ELEMENT field (msg|arg|var)*>
 <!ATTLIST field property CDATA #REQUIRED>
 <!ATTLIST field depends CDATA #IMPLIED>
 <!ATTLIST field page CDATA #IMPLIED>
 <!ATTLIST field indexedListProperty CDATA #IMPLIED>
+<!ATTLIST field stopOnFail CDATA #IMPLIED>
 
-
 <!--
      Defines a custom message key to use when one of the
      validators for this field fails. Each validator has a default message
Index: D:/workspace3.1/trunk/src/share/org/apache/commons/validator/Field.java
===================================================================
--- D:/workspace3.1/trunk/src/share/org/apache/commons/validator/Field.java     
(revision 475615)
+++ D:/workspace3.1/trunk/src/share/org/apache/commons/validator/Field.java     
(working copy)
@@ -90,6 +90,12 @@
      * The Field's indexed list property name.
      */
     protected String indexedListProperty = null;
+    
+    /**
+     * Defines if all indexed fields should be validated or stop
+     * validation should stop on first fail.
+     */
+    protected boolean stopOnFail = true;
 
     /**
      * The Field's unique key.
@@ -216,8 +222,26 @@
     public String getIndexedListProperty() {
         return this.indexedListProperty;
     }
+    
+    /**
+     * Gets a boolean value that defines if all index values should
+     * be validated or validation should stop on the first
+     * index failing (Default: true)
+     * @return true if validation stops on first fail.  
+     */
+    public boolean isStopOnFail() {
+      return stopOnFail;
+    }
 
     /**
+     * Sets the stopOnFail boolean attribute of the field.
+     * @param stopOnFail a boolean value that defines if validation should 
stop on first fail
+     */
+    public void setStopOnFail(boolean stopOnFail) {
+      this.stopOnFail = stopOnFail;
+    }
+
+    /**
      * Sets the indexed property name of the field.
      * @param indexedListProperty The field's indexed List property name.
      */
@@ -881,7 +905,8 @@
 
                 if (!good) {
                     allResults.merge(results);
-                    return allResults;
+                    if (isStopOnFail()) return allResults;
+                    else break;
                 }
             }
             allResults.merge(results);
Index: 
D:/workspace3.1/trunk/src/share/org/apache/commons/validator/ValidatorAction.java
===================================================================
--- 
D:/workspace3.1/trunk/src/share/org/apache/commons/validator/ValidatorAction.java
   (revision 475615)
+++ 
D:/workspace3.1/trunk/src/share/org/apache/commons/validator/ValidatorAction.java
   (working copy)
@@ -34,6 +34,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.StringTokenizer;
+import java.util.Iterator;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -741,6 +742,39 @@
                 Field.TOKEN_INDEXED,
                 "[" + pos + "]"));
 
+        // replace index token within key of args
+        for (int i=0; i<indexedField.args.length; i++) {
+            Map argMap = indexedField.args[i];
+            Iterator iter = argMap.values().iterator();
+            while (iter.hasNext()) {
+                Arg arg = (Arg) iter.next();
+                arg.setKey(ValidatorUtils.replace(
+                    arg.getKey(),
+                    Field.TOKEN_INDEXED,
+                    "[" + pos + "]"));
+            }
+        }
+        
+        // replace index token within key of msg
+        Iterator msgIter = indexedField.hMsgs.values().iterator();
+        while (msgIter.hasNext()) {
+            Msg msg = (Msg) msgIter.next();
+            msg.setKey(ValidatorUtils.replace(
+                msg.getKey(),
+                Field.TOKEN_INDEXED,
+                "[" + pos + "]"));
+        }
+        
+        // replace index token within value of var
+        Iterator varIter = indexedField.hVars.values().iterator();
+        while (varIter.hasNext()) {
+            Var var = (Var) varIter.next();
+            var.setValue(ValidatorUtils.replace(
+                var.getValue(),
+                Field.TOKEN_INDEXED,
+                "[" + pos + "]"));
+        }
+        
         paramValues[fieldIndex] = indexedField;
     }
     
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to