You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Peter S. Hamlen" <ph...@mail.com> on 2002/07/08 18:13:06 UTC

Re: Doing business logic validation in the Action - ValdiatableAction

James,

I'll just mention that we've taken an alternate approach from the others
mentioned on the newsgroup.   What we did was the following:

1) Create an interface called "ValidatableAction", with a method called
"validate".  

2) Create a "ValidatingForm" (bad name, I know) - within its Validate
method, it checks the action to see if it's an instanceof
ValidatableAction.  If so, it calls the action's validate method.

3) Ensure that your forms extend ValidatingForm, and your actions
implement ValidatableAction.

This allows you to capture the validating business logic in the action's
"validate" method, and leave the business processing logic in the
action's "execute" method.

See below for some of the actual code.  I welcome comments from the
group on this stuff...

-Peter


-----------------Actual Code ------------------------------

//Code for ValidatableAction
public interface ValidatableAction
{
   
    public ActionErrors validateForm(	Form form, 
					ActionMapping mapping,
                             		HttpServletRequest request);
}



// Code within ValidatingForm's validate method

public ActionErrors validate(ActionMapping mapping, HttpServletRequest
request)
{
   Action action=(Action)Class.forName(mapping.getType()).newInstance();
   if (action instanceof ValidatableAction)
   {
	ValidatableAction validatableAction =(ValidatableAction)action;
	return validatableAction.validate(this, mapping, request);
   }
}



On Sun, 2002-07-07 at 19:40, James Turner wrote:
> Consider the following:  You have a new user creation form, and need to 
> ensure that the username chosen doesn't already exist in the database.
> 
> To keep any business logic out of the ActionForm, you'd really like to do 
> this in the Action.  However, as near as I can tell, the Action doesn't 
> have any access to the ActionErrors returned from the form, which means 
> that you'd have to use another mechanism (like a request attribute) to 
> return these type of errors.
> 
> Am I missing something, or is this the best practice for business logic 
> validation of forms?
> 
> James
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 





--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>