commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ben Avery <>
Subject [Validator] problem with dependencies
Date Mon, 18 Apr 2005 16:10:43 GMT

I'm trying to use the (not Struts) Commons Validator for a standalone 
application uploading a data file to a database, which needs to validate 
the data coming in.

Each field may have a number of Validators run on it, and I want to run 
all of them, rather than 'short circuiting' when one fails, or setting 
up dependency chains.

The problem I'm having is that I can't get all the Validators to run. 
When I get the ValidatorResult for a field, e.g. 
result.containsAction("validatorName") returns false even for 
validations I know have run.

My Code:

I'm basing my code on the sample chapter of Manning's 'Jakarta Commons' 
online bookshelf, featured on

The 'star' Validator here isn't useful - it's here to demonstrate the point.


<!DOCTYPE form-validation PUBLIC
     "-//Apache Software Foundation//DTD Commons Validator Rules 
Configuration 1.2.0//EN"

       methodParams="java.lang.Object, org.apache.commons.validator.Field"
       methodParams="java.lang.Object, org.apache.commons.validator.Field"
       methodParams="java.lang.Object, org.apache.commons.validator.Field"

     <form name="simpleform">
       <field property="title" depends="required,maxlength,star">



package org.youthnet.ben;

import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.util.ValidatorUtils;

public class ValidatorWrapper {

     public static Boolean doRequired(Object bean, Field field)
         System.out.println("in required");
         String value = ValidatorUtils.getValueAsString(bean, 
         return Boolean.valueOf(!GenericValidator.isBlankOrNull(value));

     public static Boolean doMaxlength(Object bean, Field field)
         System.out.println("in maxlength");
         Integer maxLength = Integer.decode(field.getVarValue("mlength"));
         String value = ValidatorUtils.getValueAsString(bean, 
         return Boolean.valueOf(GenericValidator.maxLength(value, 

     public static Boolean doContainsStar(Object bean, Field field)
         System.out.println("in star");
         String val = ValidatorUtils.getValueAsString(bean, 
         boolean returnVal = (val.indexOf('*') == -1) ? false : true;
         return Boolean.valueOf(!returnVal);


package org.youthnet.ben;

import org.apache.commons.validator.Validator;
import org.apache.commons.validator.ValidatorResources;
import org.apache.commons.validator.ValidatorResult;
import org.apache.commons.validator.ValidatorResults;

public class ValidatorSimple {

     public static void main(String[] args) throws Exception
         // load targeted rules
         ValidatorResources resources = new ValidatorResources(
                 new FileInputStream("validatorsimple.xml"));

         // accept user data
         Object userData = simulateUserData();

         // create new Validator instance
         Validator validator = new Validator(resources, "simpleform");
         validator.setParameter(Validator.BEAN_PARAM, userData);

         // perform validation
         ValidatorResults results = validator.validate();

         // return results for the field
         ValidatorResult result1 = results.getValidatorResult("title");
         System.out.println("Contains required: 
         System.out.println("required passed: 
         System.out.println("Contains maxlength: 
         System.out.println("maxlength passed: 
         System.out.println("Contains star: 
         System.out.println("star passed: "+result1.isValid("star"));

     private static Object simulateUserData()
         VacancyBean vac = new VacancyBean();
         vac.setTitle("Test Vacancy");
         return vac;


~~ just contains one String field, title, with a getter & 


running ValidatorSimple gives the following output:

in required
in maxlength
in star
Contains required: false
required passed: false
Contains maxlength: false
maxlength passed: false
Contains star: true
star passed: false

so, all three Validators run, but only the last one is held in the 
results, using ValidatorResult.containsAction(String name)
I would expect it to contain all three actions, with 'required' and 
'maxlength' true, and 'star' false.

I have been able to get more than one hAction in the the ValidatorResult 
for the key 'title' by using dependencies, e.g. if I add the attribute
in the global declaration for the Validator, then all three validations 
are held in the ValidatorResult.

But if any validations fail, the others are short circuited.

The behaviour I want is the the validations are not dependent on each 
other, and I can get results for any combinations of Validations on a field.

Can anyone help? Thanks in advance.


Ben Avery
Software Developer
Youthnet UK

phone: 020 7288 7333
snail: 2-3 Upper Street
        London N1 0PQ

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message