You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Scott Van Wart <sc...@indosoft.ca> on 2006/02/24 02:29:30 UTC

Validation against database: Form or Action?

I have a number of scenarios where I need to validate against a set of 
rows in a database table.  For example, when logging in, it's all fine 
and dandy to make sure a username and password is entered in the 
ActionForm.validate method, but should database access go in the 
ActionForm?  If so, how do I return errors/messages back to the calling 
page from within the Action.execute method?

- Scott

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


Re: Validation against database: Form or Action?

Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
Action has some convenience methods for saving errors - you can do something
like the following in your Action's execute method:

    if (<insert-condition-here>) {
        ActionMessages errors = new ActionMessages();
        errors.add("myProperty",
                   new ActionMessage("myProperty.error"));
        saveErrors(request, errors);
        return mapping.getInputForward();
    }

This will work with the latest version of Struts - it  may vary depending on
what verison you're using.

Niall

----- Original Message ----- 
From: "Scott Van Wart" <sc...@indosoft.ca>
Sent: Friday, February 24, 2006 1:29 AM


> I have a number of scenarios where I need to validate against a set of
> rows in a database table.  For example, when logging in, it's all fine
> and dandy to make sure a username and password is entered in the
> ActionForm.validate method, but should database access go in the
> ActionForm?  If so, how do I return errors/messages back to the calling
> page from within the Action.execute method?
>
> - Scott



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


Re: Validation against database: Form or Action?

Posted by Keith Sader <ks...@gmail.com>.
No, no database access in the action form - yech, bleh, ptui! :-)

General principle: Form > Action > Business Layer > DAO > Database.

Errors and messages from the lower layers can be handled via the
Action.saveMessages() method, then exploited via the
<html:messages><bean:write></html:messages> structures in the .jsp.

hth,

On 2/23/06, Scott Van Wart <sc...@indosoft.ca> wrote:
> I have a number of scenarios where I need to validate against a set of
> rows in a database table.  For example, when logging in, it's all fine
> and dandy to make sure a username and password is entered in the
> ActionForm.validate method, but should database access go in the
> ActionForm?  If so, how do I return errors/messages back to the calling
> page from within the Action.execute method?
>
> - Scott

--
Keith Sader
ksader@gmail.com
http://www.saderfamily.org/roller/page/ksader

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