commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mr...@apache.org
Subject svn commit: r279842 - in /jakarta/commons/proper/validator/trunk/src: share/org/apache/commons/validator/Form.java share/org/apache/commons/validator/Validator.java test/org/apache/commons/validator/ValidatorTest.java
Date Fri, 09 Sep 2005 18:38:09 GMT
Author: mrdon
Date: Fri Sep  9 11:38:07 2005
New Revision: 279842

URL: http://svn.apache.org/viewcvs?rev=279842&view=rev
Log:
Added methods to make it easier to validate a single field, particularly
useful for AJAX applications that want to validate a single field through 
a remote xmlhttprequest call.

Modified:
    jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/Form.java
    jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/Validator.java
    jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTest.java

Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/Form.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/Form.java?rev=279842&r1=279841&r2=279842&view=diff
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/Form.java
(original)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/Form.java
Fri Sep  9 11:38:07 2005
@@ -244,18 +244,51 @@
      */
     ValidatorResults validate(Map params, Map actions, int page)
         throws ValidatorException {
+        return validate(params, actions, page, null);
+    }
+    
+    /**
+     * Validate all Fields in this Form on the given page and below.
+     *
+     * @param params               A Map of parameter class names to parameter
+     *      values to pass into validation methods.
+     * @param actions              A Map of validator names to ValidatorAction
+     *      objects.
+     * @param page                 Fields on pages higher than this will not be
+     *      validated.
+     * @return                     A ValidatorResults object containing all
+     *      validation messages.
+     * @throws ValidatorException
+     * @since 1.2.0
+     */
+    ValidatorResults validate(Map params, Map actions, int page, String fieldName)
+        throws ValidatorException {
 
         ValidatorResults results = new ValidatorResults();
         params.put(Validator.VALIDATOR_RESULTS_PARAM, results);
 
-        Iterator fields = this.lFields.iterator();
-        while (fields.hasNext()) {
-            Field field = (Field) fields.next();
-
+        // Only validate a single field if specified
+        if (fieldName != null) {
+            Field field = (Field) this.hFields.get(fieldName);
+            
+            if (field == null) {
+               throw new ValidatorException("Unknown field "+fieldName+" in form "+getName());
+            }
             params.put(Validator.FIELD_PARAM, field);
-
+            
             if (field.getPage() <= page) {
-                results.merge(field.validate(params, actions));
+               results.merge(field.validate(params, actions));
+            }
+        } else {
+            Iterator fields = this.lFields.iterator();
+            while (fields.hasNext()) {
+                Field field = (Field) fields.next();
+    
+                params.put(Validator.FIELD_PARAM, field);
+    
+                if (field.getPage() <= page) {
+                    results.merge(field.validate(params, actions));
+                }
             }
         }
 

Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/Validator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/Validator.java?rev=279842&r1=279841&r2=279842&view=diff
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/Validator.java
(original)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/Validator.java
Fri Sep  9 11:38:07 2005
@@ -91,7 +91,16 @@
 
     protected ValidatorResources resources = null;
 
+    /**
+     * The name of the form to validate
+     */
     protected String formName = null;
+    
+    /**
+     * The name of the field on the form to validate
+     * @since 1.2.0
+     */
+    protected String fieldName = null;
 
     /**
      * Maps validation method parameter class names to the objects to be passed
@@ -152,6 +161,27 @@
         this.resources = resources;
         this.formName = formName;
     }
+    
+    /**
+     * 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 fieldName Key used for retrieving the set of validation rules for a field
+     * @since 1.2.0
+     */
+    public Validator(ValidatorResources resources, String formName, String fieldName) {
+        if (resources == null) {
+            throw new IllegalArgumentException("Resources cannot be null.");
+        }
+
+        this.resources = resources;
+        this.formName = formName;
+        this.fieldName = fieldName;
+    }
 
     /**
      * Set a parameter of a pluggable validation method.
@@ -190,6 +220,16 @@
     public void setFormName(String formName) {
         this.formName = formName;
     }
+    
+    /**
+     * Sets the name of the field to validate in a form (optional)
+     *
+     * @param fieldName The name of the field in a form set
+     * @since 1.2.0
+     */
+    public void setFieldName(String fieldName) {
+        this.fieldName = fieldName;
+    }
 
     /**
      * Gets the page.  This in conjunction with the page property of
@@ -220,6 +260,7 @@
      */
     public void clear() {
         this.formName = null;
+        this.fieldName = null;
         this.parameters = new HashMap();
         this.page = 0;
     }
@@ -302,7 +343,8 @@
             return form.validate(
                 this.parameters,
                 this.resources.getValidatorActions(),
-                this.page);
+                this.page,
+                this.fieldName);
         }
 
         return new ValidatorResults();

Modified: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTest.java?rev=279842&r1=279841&r2=279842&view=diff
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTest.java
(original)
+++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/ValidatorTest.java
Fri Sep  9 11:38:07 2005
@@ -146,6 +146,28 @@
         assertFalse(results.getPropertyNames().contains(property));    
    }
    
+   public void testOnlyValidateField() throws ValidatorException {
+    	//     property name of the method we are validating
+    	String property = "date";
+    	// name of ValidatorAction
+    	String action = "date";
+    	ValidatorResources resources = setupDateResources(property, action);
+    
+    	TestBean bean = new TestBean();
+    	bean.setDate("2/3/1999");
+    
+    	Validator validator = new Validator(resources, "testForm", property);
+    	validator.setParameter(Validator.BEAN_PARAM, bean);
+    
+    	ValidatorResults results = validator.validate();
+    
+    	assertNotNull(results);
+    
+        // Field passed and should be in results
+    	assertTrue(results.getPropertyNames().contains(property));
+   }
+   
+   
     private ValidatorResources setupDateResources(String property, String action) {
     
     	ValidatorResources resources = new ValidatorResources();



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message