You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Vic Cekvenich <ce...@basebeans.com> on 2003/10/01 16:34:14 UTC

Re: Looking for thoughts on html:errors with indexed properties

I  belive I had "reported" in bugzzila multi row validation problme 2 
years ago, as well as posts.
Here is the code I have been shiping for a long time with bP (writen by 
Jose/Ben):
package com.fX.base;

/*
  * MultiRowValidator.java
  * by Ben Tomasini - original by Jose Quiteriono ?
  * 12/10/2002
  *
  */

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.ValidatorAction;
import org.apache.struts.action.ActionErrors;

/**
  * This class contains multi-row validations for a collection
  * *** validation.xml
  ex: use
  <form name="ClosingChecklistForm">
  <field property="sortOrder" depends="multiRowRequired,multiRowInteger">
  <arg0 key="ClosingChecklistForm.sortOrder.displayname"/>
  <var>
  <var-name>rowName</var-name>
  <var-value>row</var-value>
  </var>
  </field>
  <field property="daysBeforeBenchmark" depends="multiRowInteger">
  <arg0 key="ClosingChecklistForm.daysBeforeBenchmark.displayname"/>
  <var>
  <var-name>rowName</var-name>
  <var-value>row</var-value>
  </var>
  </field>
  <field property="description" depends="multiRowRequired">
  <arg0 key="ClosingChecklistForm.description.displayname"/>
  <var>
  <var-name>rowName</var-name>
  <var-value>row</var-value>
  </var>
  </field>
  </form>
  *
  */
public class MultiRowValidator extends 
org.apache.struts.validator.FieldChecks
         implements Serializable {
     //TODO: rename row to index

     /**
      * Commons Logging instance.
      */
     private static Log LOG = LogFactory.getLog(MultiRowValidator.class);

     /**
      * <p>Checks if the field isn't null and length of the field is 
greater than zero not
      * including whitespace.</p>
      *
      * @param   bean        The bean - must implement Collection
      * @param   va      The <code>ValidatorAction</code> that is 
currently being performed.
      * @param   field       The <code>Field</code> object associated 
with the current field
      *              being validated.
      * @param   errors      The <code>ActionErrors</code> object to add 
errors to if any
      *                          validation errors occur.
      * @param   request         Current request object.
      */
     public static boolean validateMultiRowRequired(Object bean,
                                                    ValidatorAction va, 
Field field,
                                                    ActionErrors errors,
                                                    HttpServletRequest 
request) {

         boolean valid = true;
         // Need to iterate over a collection
         Collection coll = (Collection) bean;

         for (Iterator i = coll.iterator(); i.hasNext();) {

             Object entry = (Object) i.next();
             if (!validateRequired(entry, va, field, errors, request))
                 valid = false;

         }

         return valid;

     }


     /**
      * <p>Checks if the field matches the regular expression in the 
field's mask attribute.</p>
      *
      * @param   bean        The bean - must implement collection
      * @param   va      The <code>ValidatorAction</code> that is 
currently being performed.
      * @param   field       The <code>Field</code> object associated 
with the current field
      *              being validated.
      * @param   errors      The <code>ActionErrors</code> object to add 
errors to if any
      *                          validation errors occur.
      * @param   request         Current request object.
      */
     public static boolean validateMultiRowMask(Object bean,
                                                ValidatorAction va, 
Field field,
                                                ActionErrors errors,
                                                HttpServletRequest 
request) {

         boolean valid = true;
         // Need to iterate over a collection
         Collection coll = (Collection) bean;

         for (Iterator i = coll.iterator(); i.hasNext();) {

             Object entry = (Object) i.next();
             if (!validateMask(entry, va, field, errors, request))
                 valid = false;

         }

         return valid;

     }


     /**
      * <p>Checks if the field can safely be converted to an int 
primitive.</p>
      *
      * @param   bean        The bean - must implement collection
      * @param   va      The <code>ValidatorAction</code> that is 
currently being performed.
      * @param   field       The <code>Field</code> object associated 
with the current field
      *              being validated.
      * @param   errors      The <code>ActionErrors</code> object to add 
errors to if any
      *                          validation errors occur.
      * @param   request         Current request object.
      */
     public static boolean validateMultiRowInteger(Object bean,
                                                   ValidatorAction va, 
Field field,
                                                   ActionErrors errors,
                                                   HttpServletRequest 
request) {

         boolean valid = true;
         // Need to iterate over a collection
         Collection coll = (Collection) bean;

         for (Iterator i = coll.iterator(); i.hasNext();) {

             Object entry = (Object) i.next();
             if (validateInteger(entry, va, field, errors, request) != null)
                 valid = false;

         }

         return valid;
     }

     /**
      * <p>Checks if the field can safely be converted to a float 
primitive.</p>
      *
      * @param   bean        The bean - must implement collection
      * @param   va      The <code>ValidatorAction</code> that is 
currently being performed.
      * @param   field       The <code>Field</code> object associated 
with the current field
      *              being validated.
      * @param   errors      The <code>ActionErrors</code> object to add 
errors to if any
      *                          validation errors occur.
      * @param   request         Current request object.
      */
     public static boolean validateMultiRowFloat(Object bean,
                                                 ValidatorAction va, 
Field field,
                                                 ActionErrors errors,
                                                 HttpServletRequest 
request) {

         boolean valid = true;
         // Need to iterate over a collection
         Collection coll = (Collection) bean;

         for (Iterator i = coll.iterator(); i.hasNext();) {

             Object entry = (Object) i.next();
             if (validateFloat(entry, va, field, errors, request) != null)
                 valid = false;

         }

         return valid;

     }

     /**
      * <p>Checks if a fields value is within a range (min &amp; max 
specified
      * in the vars attribute).</p>
      *
      * @param   bean        The bean - must implement collection
      * @param   va      The <code>ValidatorAction</code> that is 
currently being performed.
      * @param   field       The <code>Field</code> object associated 
with the current field
      *              being validated.
      * @param   errors      The <code>ActionErrors</code> object to add 
errors to if any
      *                          validation errors occur.
      * @param   request         Current request object.
      */
     public static boolean validateMultiRowRange(Object bean,
                                                 ValidatorAction va, 
Field field,
                                                 ActionErrors errors,
                                                 HttpServletRequest 
request) {


         boolean valid = true;
         // Need to iterate over a collection
         Collection coll = (Collection) bean;

         for (Iterator i = coll.iterator(); i.hasNext();) {

             Object entry = (Object) i.next();
             if (!validateIntRange(entry, va, field, errors, request))


                 valid = false;

         }

         return valid;

     }


     /**
      * <p>Return <code>true</code> if the specified object is a String or
      * a <code>null</code> value.</p>
      *
      * @param o Object to be tested
      */
     protected static boolean isString(Object o) {

         if (o == null) {
             return (true);
         }
         return (String.class.isInstance(o));

     }


}


Also needded is attached valdation XMl that should be used in addition 
to other valdiation xmls. Let me know if you nned more suggestions, etc. 
  for how we have gotten arround it.

hth,
.V

James Turner wrote:
> Hi gang,
>    Currently, the html:errors tag doesn't work well with
> indexed properties, because it looks for an exact match on
> the property name.  So if you have an error in
> petFish[3].species, you need to have:
> <html:errors property="petFish[3].species">
> 
> I'd like to propose (and will implement if people think it's
> a good idea) two new things.
> 
> First off, doing <html:errors name="petFish"
> property="species" indexed="true"> should match errors for
> the current iteration values.
> 
> Secondly, doing <html:errors name="petFish"
> property="species"> should match all errors for any
> iteration.
> 
> James