You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Caroline Jen <ji...@yahoo.com> on 2004/10/12 17:14:54 UTC

Write Own Validation Instead of Using validation.xml

I am submitteing a form.  This form provides check
boxes for multiple selections.  I am not using the
validation.xml to validate if "none" is selected. 
Instead, I am going to use the validate().

1. Do I write this validate() in the form class that
extends the ActionForm?  

2. how do I write this validate() method?

3. Where does this 'ae' return to?


public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) 
{
   ActionErrors ae = new ActionErrors();

   if ????? 
   {
      ae.add( "???", new ActionError("a lable that
will be inserted into the application.properties
file"));
   }
   return ae;
}

My form class looks like:

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

public class selectRecipientsForm extends ActionForm
{
    private Map recipientFieldMap = new HashMap();

    private String[] recipients = new String[0];

    public RecipientField getRecipients( int index )
    {
       RecipientField recipientField = (
RecipientField ) recipientFieldMap.get( new Integer(
index ) );
       if ( recipientField == null )
       {
         recipientField = new RecipientField();
         recipientFieldMap.put( new Integer( index ),
recipientField );
       }
       return recipientField;
    }

   public void reset( ActionMapping mapping,
HttpServletRequest request) 
   {
       recipients = new String[0];
   }
}


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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


Re: Write Own Validation Instead of Using validation.xml

Posted by Jeff Beal <jb...@webmedx.com>.
Caroline Jen wrote:
> I am submitteing a form.  This form provides check
> boxes for multiple selections.  I am not using the
> validation.xml to validate if "none" is selected. 
> Instead, I am going to use the validate().
> 
> 1. Do I write this validate() in the form class that
> extends the ActionForm?  

Yes

> 2. how do I write this validate() method?

However you want to.  Examine the fields of your form class for whatever 
validity rules your application requires.  If any rule is violated, add 
an appropriate error message into the ActionErrors message. 
Specifically for the check boxes, you can just check the length of the 
recipientFields array.

> 3. Where does this 'ae' return to?

The ActionErrors object is placed in the request as an attribute with a 
name given in Globals.ERROR_KEY.  This is where the <html:errors/> and 
<html:messages/> tags will look.  See RequestProcessor.processValidate() 
for the full Java code that handles this.
> 
> 
> public ActionErrors validate(ActionMapping mapping,
> 			HttpServletRequest request) 
> {
>    ActionErrors ae = new ActionErrors();
> 
>    if ????? 
>    {
>       ae.add( "???", new ActionError("a lable that
> will be inserted into the application.properties
> file"));
>    }
>    return ae;
> }
> 
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com 


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