You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Domingo A. Rodriguez S." <do...@yahoo.com> on 2004/05/01 03:26:23 UTC

Re: Array (or List, or Map) Validation

my problem..

I have a list-backed actionform...
That list can have more than one locationObject javabean...
The locationObject javabean has some fields, including an errorFormat
field..
That errorFormat field can be set using a mutator method from the javabean
called setErrorFormat("BACKGROUND:#000000;COLOR:#FFFFFF;")..
I am not using the validate method of the actionForm..

my solution..
from inside an Action I do the following..

LocationObjectForm lof= (LocationObjectForm)form;
LDHelper ldh= LDHelper();
LocationObject lob= new LocationObject();
List locations= lof.getLocations();
Iterator cnt= locations.iterator();
boolean isDirty= false;
while(cnt.hasNext()){
  lob= (LocationObject)cnt.next();
  if(!ldh.validateRecord(lob.getCompareField1, lob.getCompareField2)){
      // if the record is not correct, 
      // then i set the errorformat css style
      lob.setErrorFormat("BACKGROUND:#000000;COLOR:#FFFFFF;");
      isDirty= true;
  }else{
      // i dont want any error format for good records.. 
      lob.setErrorFormat("");
  }

  // put the checked list back to the session
  req.getSession().setAttribute("locations", locations);

  if(isDirty){
      // forwarded to the same page or tile definition.. 
      // just to refresh and show the error style where needed..
     return map.findForward("refreshThisDefinition");
  }
  // if there is no problem then jump to the next definition or page..
   return map.findForward("nextDefinition");
}

then.. in the page where i display the records..

<table>
<logic:iterate name=locationform property=locations id=lob indexId=cnt>
<%
 List locations= locationform.getLocations();
 LocationObject lob= locations.get( cnt.intValue() );
 String estilo= lob.getErrorFormat();
%>
<tr>
<td>
<html:text property="<%="locations["+cnt+"].campo1" style='<%=estilo%>'/>
</td>
<td>
<html:text property="<%="locations["+cnt+"].campo2" style='<%=estilo%>'/>
</td>
<td>
<html:text property="<%="locations["+cnt+"].campo3" style='<%=estilo%>'/>
</td>
</tr>
</logic:iterate>
</table>

Using this I accomplished one of my latest tasks.. :)  I highlight the
incorrect records, so the users will see their errors easier.

Atte.
Domingo A. Rodriguez S.

 --- Niall Pemberton <ni...@blueyonder.co.uk> escribió: > To
make this kind of feature useful there needs to be some way of
> indicating
> which occurance of the "indexed" property is in error. So, for example, 
> if
> you had a bunch of "order" beans and were validating the order value I
> would
> want to be able to output a message along the lines of...
> 
>    "Order value for order number 12345 is invalid"
> or
>    "Order value on line 5 is invalid"
> 
> ... where order number or line number are other properties from the bean
> being validated. Otherwise if you had 20 errors on order value then you
> just
> get 20 indetical messages along the lines of  "Order value is invalid" -
> which isn't very useful.
> 
> I looked at trying to do this in the current validator but can't see how
> to - have a missed something or am I right and its not possible?
> 
> This is an issue for me with the existing functionality where it stops
> on
> the first indexed field in error - we got round it by outputing a
> message
> which doesn't indicate which indexed occurance is in error, but by
> highlighting the field in error, which works reasonably well.
> 
> That also would be another type of solution, validate all the indexed
> properties, only show one generic message but highlight all the fields
> in
> error.
> 
> I'd be happy if someone would either put me right on this, or say how
> they
> dealt with this scenario.
> 
> Niall
> 
> 
> ----- Original Message ----- 
> From: "Robert Leland" <rl...@apache.org>
> To: "Struts Users Mailing List" <us...@struts.apache.org>
> Sent: Friday, April 30, 2004 1:12 AM
> Subject: Re: Array Validation
> 
> 
> > > -----Original Message Slightly rearranged -----
> > > From: Glenn, Scott [mailto:Scott.Glenn@umusic.com]
> > >
> > > If there are any Struts developers listening can you explain this ..
> is
> it a
> > > bug or by design?
> >
> > Doesn't matter. What matters is that it's not the behavior you need
> right
> ?
> > If someone would like to develop and test a patch that adds an option
> to:
> >   A) The <PlugIn > XML element in the struts-config.xml
> >       This would change the default behavior to not stop validating on
> an
> >       error for a given module.
> > or
> >   B) The Validator.xml itself, which would require a change to the
> >      validator.dtd, either to:
> >      B1) The form definition, so the behavior could be changed on a
> >          per form basis.
> >          The Option (whenInvalid ? ) would probably have four possible
> >          values. [stop, inherit, all].
> >             stop = This is the current default behavior
> >             go   = This is the behavior you want, where it doesn't
> stop
> >                        at the first error but attempts to validate all
> data.
> >             inherit = This would inherit the behavior from the parent
> form.
> >                       This assumes using Validator 1.2 which has a
> >                       validator form inheritance.
> >             module = Defers to what the modules default behavior is,
> >                      assuming that option A) is also implemented.
> > or
> >      B2) The validator definition itself so it could be changed on a
> >          per type basis.
> >
> >
> > Of these A) Is the simplest and hence the fastest to develop and hence
> test.
> >             Because it?s the fastest it?s more likely to be in the
> >             1.2.1 release because.
> >
> >
> >   Long term Option B1) probably makes the most sense, but since it
> >   requires a change to the commons Validator DTD, it won't make it
> into
> >   Struts 1.2.1 since that will be using validator 1.1.3 which will be
> >   released this weekend.
> >
> > The patch should include a unit test showing that the default
> behaviour
> > in the same as in Struts 1.1, along with tests for each option.
> >
> >
> >
> > > Thanks,
> > >
> > > Scott.
> > >
> > >
> > >
> > >
> > > FYI: Looks like this is a bug/feature of Struts validation.  If you
> have
> a
> > > List of indexed properties, it stops validating them once it has
> discovered
> > > the first error.
> > >
> > > The code below is from the Validator class - it loops around all
> indexed
> > > fields (indexedList), calling the appropriate validate rule
> > > (validateFieldForRule()).  However if this validate rule return
> false
> into
> > > the "good" variable, then the method exits without completing the
> loop.
> > >
> > > for (int pos = 0; pos < indexedList.length; pos++)
> > > {
> > >    ValidatorResults results = new ValidatorResults();
> > >    StringTokenizer st = new StringTokenizer(field.getDepends(),
> ",");
> > >    while (st.hasMoreTokens()) {
> > >        String depend = st.nextToken().trim();
> > >        ValidatorAction action = (ValidatorAction)
> actions.get(depend);
> > >        if (action == null) {
> > >            log.error("No ValidatorAction called "
> > >                       + depend
> > >                       + " found for field "
> > >                       + field.getProperty());
> > >            return;
> > >       }
> > >
> > >       boolean good = validateFieldForRule(field, action, results,
> actions,
> > > pos);
> > >       allResults.merge(results);
> > >       if (!good)
> > > {
> > >          return;
> > >       }
> > >   }
> > >
> >
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
> >
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>  

_________________________________________________________
Do You Yahoo!?
Información de Estados Unidos y América Latina, en Yahoo! Noticias.
Visítanos en http://noticias.espanol.yahoo.com

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


Re: Array (or List, or Map) Validation

Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
My solution to highlight error fields was to create a custom <html:text> tag
which checks if the property associated with the tag has an error in the
ActionErrors object - other than the tag I use the standard struts
validation - nothing extra required in the Actions. Source code is here:

 http://www.niallp.pwp.blueyonder.co.uk/#errortag

Niall

----- Original Message ----- 
From: "Domingo A. Rodriguez S." <do...@yahoo.com>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Saturday, May 01, 2004 2:26 AM
Subject: Re: Array (or List, or Map) Validation


>
> my problem..
>
> I have a list-backed actionform...
> That list can have more than one locationObject javabean...
> The locationObject javabean has some fields, including an errorFormat
> field..
> That errorFormat field can be set using a mutator method from the javabean
> called setErrorFormat("BACKGROUND:#000000;COLOR:#FFFFFF;")..
> I am not using the validate method of the actionForm..
>
> my solution..
> from inside an Action I do the following..
>
> LocationObjectForm lof= (LocationObjectForm)form;
> LDHelper ldh= LDHelper();
> LocationObject lob= new LocationObject();
> List locations= lof.getLocations();
> Iterator cnt= locations.iterator();
> boolean isDirty= false;
> while(cnt.hasNext()){
>   lob= (LocationObject)cnt.next();
>   if(!ldh.validateRecord(lob.getCompareField1, lob.getCompareField2)){
>       // if the record is not correct,
>       // then i set the errorformat css style
>       lob.setErrorFormat("BACKGROUND:#000000;COLOR:#FFFFFF;");
>       isDirty= true;
>   }else{
>       // i dont want any error format for good records..
>       lob.setErrorFormat("");
>   }
>
>   // put the checked list back to the session
>   req.getSession().setAttribute("locations", locations);
>
>   if(isDirty){
>       // forwarded to the same page or tile definition..
>       // just to refresh and show the error style where needed..
>      return map.findForward("refreshThisDefinition");
>   }
>   // if there is no problem then jump to the next definition or page..
>    return map.findForward("nextDefinition");
> }
>
> then.. in the page where i display the records..
>
> <table>
> <logic:iterate name=locationform property=locations id=lob indexId=cnt>
> <%
>  List locations= locationform.getLocations();
>  LocationObject lob= locations.get( cnt.intValue() );
>  String estilo= lob.getErrorFormat();
> %>
> <tr>
> <td>
> <html:text property="<%="locations["+cnt+"].campo1" style='<%=estilo%>'/>
> </td>
> <td>
> <html:text property="<%="locations["+cnt+"].campo2" style='<%=estilo%>'/>
> </td>
> <td>
> <html:text property="<%="locations["+cnt+"].campo3" style='<%=estilo%>'/>
> </td>
> </tr>
> </logic:iterate>
> </table>
>
> Using this I accomplished one of my latest tasks.. :)  I highlight the
> incorrect records, so the users will see their errors easier.
>
> Atte.
> Domingo A. Rodriguez S.
>
>  --- Niall Pemberton <ni...@blueyonder.co.uk> escribió: > To
> make this kind of feature useful there needs to be some way of
> > indicating
> > which occurance of the "indexed" property is in error. So, for example,
> > if
> > you had a bunch of "order" beans and were validating the order value I
> > would
> > want to be able to output a message along the lines of...
> >
> >    "Order value for order number 12345 is invalid"
> > or
> >    "Order value on line 5 is invalid"
> >
> > ... where order number or line number are other properties from the bean
> > being validated. Otherwise if you had 20 errors on order value then you
> > just
> > get 20 indetical messages along the lines of  "Order value is invalid" -
> > which isn't very useful.
> >
> > I looked at trying to do this in the current validator but can't see how
> > to - have a missed something or am I right and its not possible?
> >
> > This is an issue for me with the existing functionality where it stops
> > on
> > the first indexed field in error - we got round it by outputing a
> > message
> > which doesn't indicate which indexed occurance is in error, but by
> > highlighting the field in error, which works reasonably well.
> >
> > That also would be another type of solution, validate all the indexed
> > properties, only show one generic message but highlight all the fields
> > in
> > error.
> >
> > I'd be happy if someone would either put me right on this, or say how
> > they
> > dealt with this scenario.
> >
> > Niall
> >
> >
> > ----- Original Message ----- 
> > From: "Robert Leland" <rl...@apache.org>
> > To: "Struts Users Mailing List" <us...@struts.apache.org>
> > Sent: Friday, April 30, 2004 1:12 AM
> > Subject: Re: Array Validation
> >
> >
> > > > -----Original Message Slightly rearranged -----
> > > > From: Glenn, Scott [mailto:Scott.Glenn@umusic.com]
> > > >
> > > > If there are any Struts developers listening can you explain this ..
> > is
> > it a
> > > > bug or by design?
> > >
> > > Doesn't matter. What matters is that it's not the behavior you need
> > right
> > ?
> > > If someone would like to develop and test a patch that adds an option
> > to:
> > >   A) The <PlugIn > XML element in the struts-config.xml
> > >       This would change the default behavior to not stop validating on
> > an
> > >       error for a given module.
> > > or
> > >   B) The Validator.xml itself, which would require a change to the
> > >      validator.dtd, either to:
> > >      B1) The form definition, so the behavior could be changed on a
> > >          per form basis.
> > >          The Option (whenInvalid ? ) would probably have four possible
> > >          values. [stop, inherit, all].
> > >             stop = This is the current default behavior
> > >             go   = This is the behavior you want, where it doesn't
> > stop
> > >                        at the first error but attempts to validate all
> > data.
> > >             inherit = This would inherit the behavior from the parent
> > form.
> > >                       This assumes using Validator 1.2 which has a
> > >                       validator form inheritance.
> > >             module = Defers to what the modules default behavior is,
> > >                      assuming that option A) is also implemented.
> > > or
> > >      B2) The validator definition itself so it could be changed on a
> > >          per type basis.
> > >
> > >
> > > Of these A) Is the simplest and hence the fastest to develop and hence
> > test.
> > >             Because it?s the fastest it?s more likely to be in the
> > >             1.2.1 release because.
> > >
> > >
> > >   Long term Option B1) probably makes the most sense, but since it
> > >   requires a change to the commons Validator DTD, it won't make it
> > into
> > >   Struts 1.2.1 since that will be using validator 1.1.3 which will be
> > >   released this weekend.
> > >
> > > The patch should include a unit test showing that the default
> > behaviour
> > > in the same as in Struts 1.1, along with tests for each option.
> > >
> > >
> > >
> > > > Thanks,
> > > >
> > > > Scott.
> > > >
> > > >
> > > >
> > > >
> > > > FYI: Looks like this is a bug/feature of Struts validation.  If you
> > have
> > a
> > > > List of indexed properties, it stops validating them once it has
> > discovered
> > > > the first error.
> > > >
> > > > The code below is from the Validator class - it loops around all
> > indexed
> > > > fields (indexedList), calling the appropriate validate rule
> > > > (validateFieldForRule()).  However if this validate rule return
> > false
> > into
> > > > the "good" variable, then the method exits without completing the
> > loop.
> > > >
> > > > for (int pos = 0; pos < indexedList.length; pos++)
> > > > {
> > > >    ValidatorResults results = new ValidatorResults();
> > > >    StringTokenizer st = new StringTokenizer(field.getDepends(),
> > ",");
> > > >    while (st.hasMoreTokens()) {
> > > >        String depend = st.nextToken().trim();
> > > >        ValidatorAction action = (ValidatorAction)
> > actions.get(depend);
> > > >        if (action == null) {
> > > >            log.error("No ValidatorAction called "
> > > >                       + depend
> > > >                       + " found for field "
> > > >                       + field.getProperty());
> > > >            return;
> > > >       }
> > > >
> > > >       boolean good = validateFieldForRule(field, action, results,
> > actions,
> > > > pos);
> > > >       allResults.merge(results);
> > > >       if (!good)
> > > > {
> > > >          return;
> > > >       }
> > > >   }
> > > >
> > >
> > >
> > >
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > > For additional commands, e-mail: user-help@struts.apache.org
> > >
> > >
> > >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
>
> _________________________________________________________
> Do You Yahoo!?
> Información de Estados Unidos y América Latina, en Yahoo! Noticias.
> Visítanos en http://noticias.espanol.yahoo.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>



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