Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

clearing HTML form fields after return from struts validation with errors

 
Kareem Gad
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This must be really simple but I just can't get the hang of it.

I have a normal struts form that performs the validate method, if there are errors it returns back to the same jsp page that has the HTML form. Struts being lovely also pre-populates the fields with the data from the Struts Form object now in session.

I need to click on the "reset" or "clear" button on that form and be able to clear out all the fields of the form just like standard HTML reset behaviour.

When I try that the fields that were pre-populated to the HTML form do not clear out. What can i do to overcome this, other than the manual way of having a javascript that can go and clear each and every field in the form manually.

Consider a form with just too many fields to do that !

I hope this has been addressed by someone before..
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kareem,
I can think of two ways:
1) Use Javascript to loop through the form fields instead of hard coding each one.
2) Redirect to the Struts action that created the blank form in the first place.
 
kapil patel
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have similar problem. I am reseting form using javascript. However, it displays again to all previously entered field values.

In my case, I am populating some selection options using another action. And submit jsp page for another action.

thanks in advance
kapil patel
 
Christian Nash
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kareem,

From what you say I'm guessing there are 2 forms and your fields are distributed between these forms.

in the javascript you can use:

for the upper form if the elements are all text fields.
Similarly for the lower form you can use

 
Kareem Gad
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Thanks for your suggestions. I have come around resolving this using the approach suggested by Jeanne of redirecting to the action that created the form blank in the first place.

Ofcourse needing to handle clearing the session appropriately when doing so.

-K
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic