You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Ben Avery <be...@youthnetuk.org> on 2005/04/18 18:10:43 UTC

[Validator] problem with dependencies

hi,

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 www.theserverside.com

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

simplevalidation.xml:

<!DOCTYPE form-validation PUBLIC
     "-//Apache Software Foundation//DTD Commons Validator Rules 
Configuration 1.2.0//EN"
     "http://jakarta.apache.org/commons/dtds/validator_1_2_0.dtd">
<form-validation>

  <global>
     <validator
       name="required"
       classname="org.youthnet.ben.ValidatorWrapper"
       method="doRequired"
       methodParams="java.lang.Object, org.apache.commons.validator.Field"
       msg=""/>
    <validator
       name="maxlength"
       classname="org.youthnet.ben.ValidatorWrapper"
       method="doMaxlength"
       methodParams="java.lang.Object, org.apache.commons.validator.Field"
       msg=""/>
     <validator
       name="star"
       classname="org.youthnet.ben.ValidatorWrapper"
       method="doContainsStar"
       methodParams="java.lang.Object, org.apache.commons.validator.Field"
       msg=""/>
   </global>

   <formset>
     <form name="simpleform">
       <field property="title" depends="required,maxlength,star">
         <var>
           <var-name>mlength</var-name>
           <var-value>20</var-value>
         </var>
       </field>
     </form>
   </formset>

</form-validation>

~~~~

ValidatorWrapper.java:


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, 
field.getProperty());
         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, 
field.getProperty());
         return Boolean.valueOf(GenericValidator.maxLength(value, 
maxLength.intValue()));
     }

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


~~~

ValidatorSimple.java:


package org.youthnet.ben;

import java.io.FileInputStream;
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: 
"+result1.containsAction("required"));
         System.out.println("required passed: 
"+result1.isValid("required"));
         System.out.println("Contains maxlength: 
"+result1.containsAction("maxlength"));
         System.out.println("maxlength passed: 
"+result1.isValid("maxlength"));
         System.out.println("Contains star: 
"+result1.containsAction("star"));
         System.out.println("star passed: "+result1.isValid("star"));
     }

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

}

~~

VacancyBean.java just contains one String field, title, with a getter & 
setter.

~~

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
depends="required,maxlength"
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

email: ben@youthnetuk.org
phone: 020 7288 7333
snail: 2-3 Upper Street
        London N1 0PQ
_________________________

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