I have done some work on the validator. These are the problems that I have tried to 
address with the attached patches and interface:

1. The current FieldChecks in Struts is receiving too much information (i.e. ActionErrors and 
HttpServletRequest). Neither of these objects is needed for the actual validation--they are both 
used to let the framework know when a validation error occurs. There is some confusion here 
between the task of validating a field and "publishing" the fact that validation 
failures have occurred. I have come up with a way to separate these tasks--the attached 
org.apache.commons.validator.integration.ValidationResultPublisher interface provides a way for 
Validator users to easily handle validation results. The user will need implement this interface 
and pass an instance of it to the validator before invoking Validator.validate(). Dependencies 
such as HttpServletRequest and ActionErrors can be provided to the ValidationResultPublisher 
before it is passed to the Validator.

2. ValidatorResult does not provide all error information (e.g. the default error 
message associated with the ValidatorAction). Consequently, I am forced to pass the 
Field and ValidatorAction to my ValidationResultPublisher. Would it be 
acceptable/helpful to provide a Resources class like 
org.springframework.validation.commons.Resources that provides helper methods for 
extracting message keys and arguments from Field and ValidatorAction objects?

I have changed my original "publisher" idea from ErrorPublisher to 
ValidationResultPublisher. That way both success and failure can be observed by the user. One 
use case for this is auditing, which could be very useful to detect that all desired validations 
are occurring. I expect most people to simply detect and publish failures, ignoring successful 
validations.

At this point, FieldChecks (or something like it) no longer needs to worry about 
framework specific error handling--it just needs to relay the result of the validation 
(true or false; success or failure).

The included patches and interface should be all you need. Let me know if you have any ideas on 
how it could be refined or improved. I'd be especially grateful if anyone could think of a 
better name than "ValidationResultPublisher"--I can't.

Thanks,
Daniel Miller
/*
 * Created on Jun 29, 2004
 */
package org.apache.commons.validator.integration;

import org.apache.commons.validator.Field;
import org.apache.commons.validator.ValidatorAction;

/**
 * This interface is provided to allow custom validation result handling. A
 * typical implementation of this interface will send an error message
 * to the presentation layer on each validation failure. Another possibility
 * is validation auditing (validation successes and/or failures may be audited).
 * 
 * @author Daniel Miller
 */
public interface ValidationResultPublisher {
  /**
   * Publish error details.
   *
   * @param field The Field that was validated.
   * @param va The ValidatorAction that was performed.
   * @param valid The status of the validation (true=success, false=failed).
   * This will always be false if the <code>Validator</code>'s
   * <code>onlyReturnErrors</code> property is set to true.
   * @param result The value returned by the validation method (appropriately
   * wrapped if primitive).
   */
        void publishResult(Field field, ValidatorAction va, boolean valid, Object 
result);
}
Index: 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.36
diff -u -r1.36 Validator.java
--- src/share/org/apache/commons/validator/Validator.java       8 Jun 2004 14:57:42 
-0000       1.36
+++ src/share/org/apache/commons/validator/Validator.java       1 Jul 2004 03:34:08 
-0000
@@ -26,6 +26,8 @@
 import java.util.Locale;
 import java.util.Map;
 
+import org.apache.commons.validator.integration.ValidationResultPublisher;
+
 /**
  * Validations are processed by the validate method. An instance of
  * <code>ValidatorResources</code> is used to define the validators
@@ -80,6 +82,16 @@
      */
     public static final String VALIDATOR_PARAM =
             "org.apache.commons.validator.Validator";
+    
+    /**
+     * Resources key the <code>ValidationResultPublisher</code> is stored under.
+     * If an implementation of <code>ValidationResultPublisher</code> is provided,
+     * the <code>publishResult</code> method will be invoked for each
+     * validation method invokation. If <code>onlyReturnErrors</code> is true,
+     * <code>publishResult</code> will only be invoked on validation failures. 
+     */
+    public static final String VALIDATION_RESULT_PUBLISHER_PARAM =
+                               
"org.apache.commons.validator.integration.ValidationResultPublisher";
             
     /**
      * Resources key the <code>Locale</code> is stored.
@@ -175,6 +187,24 @@
      */
     public Object getParameterValue(String parameterClassName) {
         return this.parameters.get(parameterClassName);
+    }
+    
+    /**
+     * Optional: Set the <code>ValidationResultPublisher</code> for the validator.
+     *
+     * @param publisher The instance of <code>ValidationResultPublisher</code>
+     * that will be used to publish validation errors.
+     */
+    public void setValidationResultPublisher(ValidationResultPublisher publisher) {
+       this.parameters.put(VALIDATION_RESULT_PUBLISHER_PARAM, publisher);
+    }
+    
+    /**
+     * Returns the <code>ValidationResultPublisher</code> that will be used
+     * during the processing of validations.
+     */
+    public ValidationResultPublisher getValidationResultPublisher() {
+       return (ValidationResultPublisher) 
this.parameters.get(VALIDATION_RESULT_PUBLISHER_PARAM);
     }
 
     /**
Index: src/share/org/apache/commons/validator/ValidatorAction.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorAction.java,v
retrieving revision 1.22
diff -u -r1.22 ValidatorAction.java
--- src/share/org/apache/commons/validator/ValidatorAction.java 10 Apr 2004 21:01:59 
-0000      1.22
+++ src/share/org/apache/commons/validator/ValidatorAction.java 1 Jul 2004 03:09:32 
-0000
@@ -37,6 +37,7 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.commons.validator.integration.ValidationResultPublisher;
 import org.apache.commons.validator.util.ValidatorUtils;
 
 /**
@@ -507,7 +508,11 @@
 
         params.put(Validator.VALIDATOR_ACTION_PARAM, this);
 
-        try {
+       ValidationResultPublisher publisher =
+                       (ValidationResultPublisher)params.get(
+                                       Validator.VALIDATION_RESULT_PUBLISHER_PARAM);
+
+       try {
             ClassLoader loader = this.getClassLoader(params);
             this.loadValidationClass(loader);
             this.loadParameterClasses(loader);
@@ -543,6 +548,9 @@
             boolean valid = this.isValid(result);
             if (!valid || (valid && !onlyReturnErrors(params))) {
                 results.add(field, this.name, valid, result);
+                if (publisher != null) {
+                       publisher.publishResult(field, this, valid, result);
+                }
             }
 
             if (!valid) {
@@ -561,6 +569,9 @@
                 e);
 
             results.add(field, this.name, false);
+            if (publisher != null) {
+               publisher.publishResult(field, this, false, null);
+            }
             return false;
         }
 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to