You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by James Turner <tu...@blackbear.com> on 2002/07/08 01:40:59 UTC

Doing business logic validation in the Action

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>


Re: Doing business logic validation in the Action - ValdiatableAction

Posted by "Peter S. Hamlen" <ph...@mail.com>.
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>


Re: Doing business logic validation in the Action

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Sun, 7 Jul 2002, Craig R. McClanahan wrote:

> Date: Sun, 7 Jul 2002 16:51:25 -0700 (PDT)
> From: Craig R. McClanahan <cr...@apache.org>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: Struts Users Mailing List <st...@jakarta.apache.org>
> Subject: Re: Doing business logic validation in the Action
>
>
>
> On Sun, 7 Jul 2002, James Turner wrote:
>
> > Date: Sun, 07 Jul 2002 19:40:59 -0400
> > From: James Turner <tu...@blackbear.com>
> > Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> > To: struts-user@jakarta.apache.org
> > Subject: Doing business logic validation in the Action
> >
> > 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?
> >
>
> The struts example app does a "validation check" like this in the Action
> instead of in the validate() method, for exactly the reason you describe.
> See the source for LogonAction to see how you can create and save error
> messages directly in an Action, and return to the right input page as
> defined for the current mapping.
>

Expanding on this slightly -- the most recent nightly builds have
supported new features to treat the "input" attribute of an <action>
element as the name of a <forward> instead of a context-relative path.  To
avoid having to deal with that kind of complexity inside your action,
there is a new method on ActionMapping called getInputForward() that
returns an ActionForward back to the input page, no matter how the
configuration stuff works.  So, you'll see LogonAction in the most recent
nightly builds do this:

  ActionErrors errors = new ActionErrors();
  ... test for error conditions and add ActionError instances ...
  if (!errors.empty()) {
    saveErrors(request, errors);
    return (mapping.getInputForward());
  }

You'll need nightly build 20020707 or later for this to work.

> > James
>
> Craig
>

Craig


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


Re: Doing business logic validation in the Action

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Sun, 7 Jul 2002, James Turner wrote:

> Date: Sun, 07 Jul 2002 19:40:59 -0400
> From: James Turner <tu...@blackbear.com>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: struts-user@jakarta.apache.org
> Subject: Doing business logic validation in the Action
>
> 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?
>

The struts example app does a "validation check" like this in the Action
instead of in the validate() method, for exactly the reason you describe.
See the source for LogonAction to see how you can create and save error
messages directly in an Action, and return to the right input page as
defined for the current mapping.

> James

Craig


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