You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Stefan Arentz <st...@soze.com> on 2003/03/07 15:07:13 UTC

GET/POST in Actions & Form Validation

Hi,

This is my first web app with Struts, so please be gentle :-)

I am trying to implement the following pattern:

  On GET:

   - Populate a form with fields OR show an empty form
   - Show the form

  On POST:

   - Validate the form, show it again if it has errors.

Very basic, but yet, I can't find the correct way to do this with 
Struts.

I have not seen one piece of example code that looks at the request 
method to make the distinction between showing the initial empty form 
(GET) or processing it (POST).

In my code I do basically this:

  execute()
  {
     if (request.getMethod().compareTo("GET") == 0)
     {
        ... code to setup the session ...
        return mapping.findForward("AddRecord"); // Shows the form
     }

     if (request.getMethod().compareTo("POST") == 0)
     {
        ... execute some business logic ...
        return mapping.findForward("Success"); // Goes back to were we 
came from
     }
   }

But the problem here is that when I forward to the AddRecord forward, 
the form is validated, which in case of an empty initial form will show 
errors for each field. I can set validate to false in my action 
definition but then validation is also skipped when I need it.

The forward is defined like this:

   <forward name="AddRecord" path="/addrecord.jsp" redirect="false"/>

This must be very basic, but it is very difficult to find a clear 
answer in the large amount of Struts related resources :-/

  S.


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


Re: GET/POST in Actions & Form Validation

Posted by Stefan Arentz <st...@soze.com>.
On Friday, Mar 7, 2003, at 15:13 Europe/Amsterdam, modena@dit.unitn.it 
wrote:

> You can set validate="false" and call the validation method from 
> action if you
> would..
> try it.

Ah! Good advise. I didn't think of that :-/

I also got it to work with an extra action where validation was set to 
false, but doing the validation programatically is much nicer.

Thanks!

  S.


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


Re: GET/POST in Actions & Form Validation

Posted by mo...@dit.unitn.it.
You can set validate="false" and call the validation method from action if you
would..
try it.


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