You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by John Hunt <te...@yahoo.com> on 2001/01/26 22:54:22 UTC

ActionClass + input field validation

Hi 
Suppose I have an ActionForm class for some validation
stuff and after that the associated Action class does
some more validation....
If ActionForm class validation fails then the request
is forwarded back to the input page  and the page is
redisplayed and the whatever data the user has input
would be displayed. ( I mean struts does that )
But if it is the ActionClass which did some validation
and on failure if it forwards the request to the
inputForm, then I am losing all the data which the
user has input. In such scenarios what needs to be
done???
To summarise, my question is if Action class does
validation and redirects to the input page because of
errors, how can I retain the values which the user has
input before.

__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - Buy the things you want at great prices. 
http://auctions.yahoo.com/

Re: ActionClass + input field validation

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
John Hunt wrote:

> Hi
> Suppose I have an ActionForm class for some validation
> stuff and after that the associated Action class does
> some more validation....
> If ActionForm class validation fails then the request
> is forwarded back to the input page  and the page is
> redisplayed and the whatever data the user has input
> would be displayed. ( I mean struts does that )
> But if it is the ActionClass which did some validation
> and on failure if it forwards the request to the
> inputForm, then I am losing all the data which the
> user has input. In such scenarios what needs to be
> done???
>

The Struts example application actually does exactly this -- for example, in
SaveRegistrationAction there is an extra check that the specified username is
unique when you are creating a new user.  It uses logic like this:

    ActionErrors errors = new ActionErrors();
    ... check for additional errors ...
    ... and add them to the "errors" object ...
    if (!errors.empty()) {
        saveErrors(request, errors);
        return (new ActionForward(mapping.getInput()));
    }

> To summarise, my question is if Action class does
> validation and redirects to the input page because of
> errors, how can I retain the values which the user has
> input before.
>

No, the input data is not lost.  The reason is that control is being returned to
the input form with a RequestDispatcher.forward(), and the form bean is still
present as a request or session attribute (depending on which mechanism you
chose).  Therefore, when the input page is redisplayed, the current form values
will already be there -- just as if you were processing errors caught by the
validate() method.

Craig McClanahan