You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dg...@apache.org on 2003/06/08 09:11:25 UTC
cvs commit: jakarta-commons/validator/src/share/org/apache/commons/validator Validator.java
dgraham 2003/06/08 00:11:25
Modified: validator/src/test/org/apache/commons/validator
ValidatorTest.java
validator/src/example/org/apache/commons/validator/example
ValidateExample.java
validator/src/share/org/apache/commons/validator
Validator.java
Log:
Added Validator ability to only include failed fields in the
ValidatorResults instead of all fields. ValidateExample
demonstrates how to configure this setting and a test was
added to ValidatorTest to prove it works.
PR# 20267
Revision Changes Path
1.14 +61 -30 jakarta-commons/validator/src/test/org/apache/commons/validator/ValidatorTest.java
Index: ValidatorTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/ValidatorTest.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- ValidatorTest.java 28 May 2003 04:28:00 -0000 1.13
+++ ValidatorTest.java 8 Jun 2003 07:11:24 -0000 1.14
@@ -117,31 +117,11 @@
* method being tested returns an object (<code>null</code> will be considered an error).
*/
public void testManualObject() {
- // property name of the method we are validating
- String property = "date";
- // name of ValidatorAction
- String action = "date";
-
- ValidatorResources resources = new ValidatorResources();
-
- ValidatorAction va = new ValidatorAction();
- va.setName(action);
- va.setClassname("org.apache.commons.validator.ValidatorTest");
- va.setMethod("formatDate");
- va.setMethodParams("java.lang.Object,org.apache.commons.validator.Field");
-
- FormSet fs = new FormSet();
- Form form = new Form();
- form.setName("testForm");
- Field field = new Field();
- field.setProperty(property);
- field.setDepends(action);
- form.addField(field);
- fs.addForm(form);
-
- resources.addValidatorAction(va);
- resources.addFormSet(fs);
- resources.process();
+ // 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");
@@ -183,8 +163,59 @@
fail("An exception was thrown while calling Validator.validate()");
}
-
}
+
+ public void testOnlyReturnErrors() 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");
+ validator.setParameter(Validator.BEAN_PARAM, bean);
+
+ ValidatorResults results = validator.validate();
+
+ assertNotNull(results);
+
+ // Field passed and should be in results
+ assertTrue(results.getPropertyNames().contains(property));
+
+ // Field passed but should not be in results
+ validator.setOnlyReturnErrors(true);
+ results = validator.validate();
+ assertFalse(results.getPropertyNames().contains(property));
+ }
+
+ private ValidatorResources setupDateResources(String property, String action) {
+
+ ValidatorResources resources = new ValidatorResources();
+
+ ValidatorAction va = new ValidatorAction();
+ va.setName(action);
+ va.setClassname("org.apache.commons.validator.ValidatorTest");
+ va.setMethod("formatDate");
+ va.setMethodParams("java.lang.Object,org.apache.commons.validator.Field");
+
+ FormSet fs = new FormSet();
+ Form form = new Form();
+ form.setName("testForm");
+ Field field = new Field();
+ field.setProperty(property);
+ field.setDepends(action);
+ form.addField(field);
+ fs.addForm(form);
+
+ resources.addValidatorAction(va);
+ resources.addFormSet(fs);
+ resources.process();
+
+ return resources;
+ }
/**
* Verify that one value generates an error and the other passes. The validation
1.13 +10 -4 jakarta-commons/validator/src/example/org/apache/commons/validator/example/ValidateExample.java
Index: ValidateExample.java
===================================================================
RCS file: /home/cvs/jakarta-commons/validator/src/example/org/apache/commons/validator/example/ValidateExample.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- ValidateExample.java 29 May 2003 03:34:35 -0000 1.12
+++ ValidateExample.java 8 Jun 2003 07:11:25 -0000 1.13
@@ -160,7 +160,13 @@
results = validator.validate();
printResults(bean, results, resources);
+ // Now only report failed fields
+ validator.setOnlyReturnErrors(true);
+ results = validator.validate();
+ printResults(bean, results, resources);
+
// Now everything should pass.
+ validator.setOnlyReturnErrors(false);
bean.setAge("123");
results = validator.validate();
printResults(bean, results, resources);
1.26 +32 -8 jakarta-commons/validator/src/share/org/apache/commons/validator/Validator.java
Index: Validator.java
===================================================================
RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/Validator.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- Validator.java 28 May 2003 04:30:58 -0000 1.25
+++ Validator.java 8 Jun 2003 07:11:25 -0000 1.26
@@ -209,6 +209,11 @@
* for instantiating new objects. Default is <code>false</code>.
*/
protected boolean useContextClassLoader = false;
+
+ /**
+ * Set this to true to not return Fields that pass validation. Only return failures.
+ */
+ protected boolean onlyReturnErrors = false;
/**
* Construct a <code>Validator</code> that will
@@ -508,11 +513,14 @@
Object result =
validationMethod.invoke(va.getClassnameInstance(), paramValue);
- results.add(field, va.getName(), isValid(result), result);
+ boolean valid = this.isValid(result);
+ if (!valid || (valid && !this.onlyReturnErrors)) {
+ results.add(field, va.getName(), valid, result);
+ }
- if (!this.isValid(result)) {
- return false;
- }
+ if (!valid) {
+ return false;
+ }
} catch (Exception e) {
log.error("reflection: " + e.getMessage(), e);
@@ -763,4 +771,20 @@
}
+ /**
+ * Returns true if the Validator is only returning Fields that fail validation.
+ */
+ public boolean getOnlyReturnErrors() {
+ return onlyReturnErrors;
+ }
+
+ /**
+ * Configures which Fields the Validator returns from the validate() method. Set this
+ * to true to only return Fields that failed validation. By default, validate() returns
+ * all fields.
+ */
+ public void setOnlyReturnErrors(boolean onlyReturnErrors) {
+ this.onlyReturnErrors = onlyReturnErrors;
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org