There are a number of ways to achieve this, and again (!) it's about choice, although I must say this can be confusing for newbie (and experienced!) struts users.
1. 2 Actions, 1 for 'pre' and 1 for 'post' processing 2. An 'action' parameter, with switch behaviour in the action class 3. 'isVirgin()' extension to the Action Form. Option 3 is basically an added method to the ActionForm class that allows the Action to determine if 'pre' or 'post' processing the form, without the need for an 'action' parameter. Consider the following in the Action class, where 'sf' refers to the instance of the form: // determine if pre-processing, and if so continue on to the View if (sf.isVirgin()) return mapping.findForward(ChikiConstants.ACTION_FORWARD_SUCCESS); // getting this far indicates post-processing... /** * Determines if pre or post processing the form.<br> * Criteria being null indicates pre-processing. * * @return true if pre-processing the form */ public boolean isVirgin () { return ( (criteria == null) ); } The 'isVirgin' method returns true if 'pre' processing the form, and false if 'post' processing. Option 1 gives good seperation, but can lead to an unneccesary proliferation of Action classes in large apps Option 2 achieves the same, but can lead to 'messy' Actions Option 3 needs additional logic in the form, but allows pre and post processing in the one Action - however, this doesn't extend as far as seperate actions and action parameters when you need to do more than just pre and post processing. In our applications we have used all 3 at various times. We tend to use option 3 when we just need the split between pre and post processing, eg a Search or Login action. In more complex sceanrio's we tend to use option 1 or 2. Try to determine which best fits your scenario. Depending on your context, one solution may be better than the other, but this will not be true across the board for all scenarios. Think through your real needs before choosing the solution. With regards to your exact scenario, we developed option 3 as a solution to this very situation, so I would suggest option 3 might be best suited, although you will ultimately have to decide ! Having developed option 3 and implemented it, we have found it to be the most effective way of getting around this. As I previously stated, option 1 and 2 come into their own in other situations. Hope this helps - if you need any more detail let me know, Ghoot > -----Original Message----- > From: keithBacon [mailto:[EMAIL PROTECTED]] > Sent: 21 February 2002 17:32 > To: Struts Users Mailing List > Subject: Re: form validation > > > There are various solutions. > I put a hidden variable on my form (& in the formBean) with > the form name in > it. > If it's not there I know not to validate the form data. > Some people have 2 Actions & Action classes for this but I > fear that would lead > to too much code duplication. > However some of my Action classes are getting a bit big & unwieldy. > > --- "Pritchard, Sean" <[EMAIL PROTECTED]> wrote: > > I've run into a bit of a sticking point on form validation > and I'm wondering > > whether anyone else has encountered this. I have an Action > EditUserAction > > that works with a UserForm to edit a user's data (e.g. > first name, last > > name. email, etc.). In my Action, I check the request for > a valid token. > > If the token is not valid, I get the user's data from the > database (via the > > model layer) and populate the form with it. I then forward > control to a jsp > > that displays the form. If the token is valid, I update > the user's data in > > the database (again via the model layer) with the data in > the form. So I > > use the same Action to initially populate the form and then > to process the > > submitted form. > > > > The problem I'm running into, is that I want to begin using > form validation. > > So I created a validate method that ensures the email > address is not null or > > zero-length. The problem is, that the error message is > displayed the first > > time the form is displayed (i.e. before the form is > submitted). It seems I > > should only validate the form when it is submitted rather > than the first > > time it is displayed. It is currently being validated > before the user > > submits it, so if the user has not previously submitted an > email address, > > when they first see the form, the error message "Email > address is required" > > appears. > > > > My current design would call for the form validation to be > invoked only when > > the token is valid. But Struts doesn't seem to lend itself > to validating a > > token within a form (because the token validation methods > are protected > > instance methods of Action). It feels a bit like I'm > fighting the direction > > the framework wants to go, which makes me think my design > is flawed. Any > > suggestions? Should I use two separate actions, one to > populate the form > > and one to process the submit? That would allow me to set > validation on one > > and not the other. Should I check for a valid token inside > my validate() > > method and only look for errors if the token is valid? > > > > Thanks in advance for your suggestions. > > Sean > > > > -- > > To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> > For additional commands, e-mail: <mailto:[EMAIL PROTECTED]> > ===== ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Search the archive:- http://www.mail-archive.com/struts-user%40jakarta.apache.org/ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Keith Bacon - Looking for struts work - South-East UK. phone UK 07960 011275 __________________________________________________ Do You Yahoo!? Yahoo! Sports - Coverage of the 2002 Olympic Games http://sports.yahoo.com -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]> -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>