You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Da...@nascopgh.com on 2003/05/27 18:39:42 UTC

[validator] PATCH - add switch to Validator.java to allow filtering out success results

Hi all (special shout out to Alex Chaffee!),

Here's a patch we have applied to our local tree to allow filtering out 
success results (is there a use case for knowing that your input *did* 
pass validation rules that wouldn't be better handled by trace debugging? 
No new is good news...)

AFAICT, it should be perfectly binary- and source- compatible w/ existing 
clients.

Please feel free to include it in the distribution.

There are other changes I was thinking of making as well, but thought they 
might prove to me rather more controversial:

1) In ValidatorResult, there is an unused protected member called Field -- 
unused except by the constructor, which requires clients to pass it in 
even though it will never be used. I don't know whether there are clients 
out there subclassing ValidatorResult and using this, but if there are 
good design says they should be refactored and add it to their own 
subclass.

LEGAL MUMBO-JUMBO: I have received an explicit waiver of any Nationwide 
Appraisal Services Corp. IP rights over the software expressed in the 
patch from Kevin Kelly, VP and MIS director of Nationwide Appraisal 
Services.


Re: [validator] PATCH - add switch to Validator.java to allow filtering out success results

Posted by Da...@nascopgh.com.
OK, let's try the inline approach; patch follows:

Index: 
validator/src/example/org/apache/commons/validator/example/ValidateExample.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/validator/src/example/org/apache/commons/validator/example/ValidateExample.java,v
retrieving revision 1.10
diff -u -r1.10 ValidateExample.java
--- 
validator/src/example/org/apache/commons/validator/example/ValidateExample.java 
25 May 2003 18:00:24 -0000      1.10
+++ 
validator/src/example/org/apache/commons/validator/example/ValidateExample.java 
26 May 2003 21:21:00 -0000
@@ -159,6 +159,16 @@
         bean.setAge("Too Old");
         results = validator.validate();
         printResults(bean, results, resources);
+
+        // Now we will turn on success-result filtering
+        // and re-run the same test
+        validator.setOnlyErrorFlag(true);
+        results = validator.validate();
+        printResults(bean, results, resources);
+        System.out.println("(contrast this with the report above)");
+
+        // Turn off success-result filtering for the final test
+        validator.setOnlyErrorFlag(false);
 
         // Now everything should pass.
         bean.setAge("123");
Index: validator/src/share/org/apache/commons/validator/Validator.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/validator/src/share/org/apache/commons/validator/Validator.java,v
retrieving revision 1.23
diff -u -r1.23 Validator.java
--- validator/src/share/org/apache/commons/validator/Validator.java     24 
May 2003 20:09:39 -0000 1.23
+++ validator/src/share/org/apache/commons/validator/Validator.java     26 
May 2003 21:21:00 -0000
@@ -168,6 +168,12 @@
     protected boolean useContextClassLoader = false;
 
     /**
+     * Whether or not to report only error results
+     * when validating. Default is <code>false</code>.
+     */
+    protected boolean onlyErrors = false;
+
+    /**
      * Construct a <code>Validator</code> that will
      * use the <code>ValidatorResources</code>
      * passed in to retrieve pluggable validators
@@ -194,6 +200,22 @@
     }
 
     /**
+     * Construct a <code>Validator</code> that will
+     * use the <code>ValidatorResources</code>
+     * passed in to retrieve pluggable validators
+     * the different sets of validation rules.
+     *
+     * @param resources <code>ValidatorResources</code> to use during 
validation.
+     * @param formName Key used for retrieving the set of validation 
rules.
+     * @param reportOnlyErrors Flag to turn off collecting success 
results.
+     */
+    public Validator(ValidatorResources resources, String formName, 
boolean reportOnlyErrors) {
+        this.resources = resources;
+        this.formName = formName;
+        this.onlyErrors = reportOnlyErrors;
+    }
+
+    /**
      * Add a resource to be used during the processing
      * of validations.
      *
@@ -258,6 +280,20 @@
     }
 
     /**
+     * Gets the current value of the flag to suppress success reporting.
+     */
+    public boolean getOnlyErrorFlag() {
+        return onlyErrors;
+    }
+
+    /**
+     * Sets the current value of the flag to suppress success reporting.
+     */
+    public void setOnlyErrorFlag(boolean reportOnlyErrors) {
+        this.onlyErrors = reportOnlyErrors;
+    }
+
+    /**
      * Gets the page.  This in conjunction with the page property of 
      * a <code>Field<code> can control the processing of fields. If the 
field's 
      * page is less than or equal to this page value, it will be 
processed.
@@ -461,7 +497,9 @@
             Object result =
                 validationMethod.invoke(va.getClassnameInstance(), 
paramValue);
 
-            results.add(field, va.getName(), isValid(result), result);
+            if(!onlyErrors || (onlyErrors && !isValid(result))) {
+                results.add(field, va.getName(), isValid(result), 
result);
+            }
 
             if (!this.isValid(result)) {
                 return false;