You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Matt Raible <ma...@icsynergy.com> on 2001/12/13 22:13:26 UTC

RE: form initialization

Here's a javascript function that will clear all the values in a form:


function clearForm(frmObj){
        for(var i = 0; i < frmObj.length; i++){
		if(frmObj.elements[i].type.indexOf("text") == 0){
            		frmObj.elements[i].value="";
		}
		else if (frmObj.elements[i].type.indexOf("radio") == 0){
			frmObj.elements[i].checked=false;
		}
		else if (frmObj.elements[i].type.indexOf("checkbox") == 0){
			frmObj.elements[i].checked = false;
		}
		else if (frmObj.elements[i].type.indexOf("select") == 0){
			for(var j = 0; j < frmObj.elements[i].length ; j++){
				frmObj.elements[i].options[j].selected=false;
			}
		}
	}
}

-----Original Message-----
From: Colin Sharples [mailto:sharples@nz1.ibm.com]
Sent: Thursday, December 13, 2001 1:37 PM
To: Struts Developers List
Subject: Re: form initialization


Apologies for following up a user-type question with another one, but in
the interests of (brain) bandwith I don't read the user list. Please
forgive me, I won't do it again :-)

If this approach is used, populating the form bean before the form is
displayed, then it means that hitting a "Reset" button will, rather than
clearing the form, clear out any user modifications and re-fill the form
with the values that the bean was populated with in the previous step.

Now, I know that this is the expected, or even required behaviour from the
point of view of HTML. However, when I had one site going through testing,
the tester complained that a particular form was broken because the reset
button kept on filling in the values instead of clearing them. In this
particular case, it was clear that what was required was not a "reset"
button, but a "clear all the fields" button.

So, getting to the point (eventually), I know that it's possible to do this
server-side, by having a button which will just cause the form bean to have
all its properties set to null and redirect back to the input page.
However, is there a way to do this client-side?

Regards

Colin M Sharples
I/T Architect
IBM Global Services New Zealand

email: sharples@nz1.ibm.com
phone: 64-4-5769853
mobile: 64-21-402085
fax: 64-4-5765616





                    "Craig R.
                    McClanahan"          To:     Struts Developers List
<st...@jakarta.apache.org>
                    <craigmcc@apac       cc:
                    he.org>              Subject:     Re: form
initialization
                    Sent by:
                    craigmcc@au.ib
                    m.com


                    13/12/2001
                    19:30
                    Please respond
                    to "Struts
                    Developers
                    List"







On Wed, 12 Dec 2001, Jon Burford wrote:

> Date: Wed, 12 Dec 2001 20:40:34 -0800
> From: Jon Burford <jb...@xsilogy.com>
> Reply-To: Struts Developers List <st...@jakarta.apache.org>
> To: struts-dev@jakarta.apache.org
> Subject: form initialization
>
> Greetings!
>
> I have a dynamic form which needs to be initialized from the database.
> If I put code to initialize the attributes in the default constructor
> of the form bean, all this happens with no problems - the form is
> displayed with the proper values.  My problem is that the form bean
> needs some of the request parameters from the calling jsp page in
> order to properly initialize its fields.  What is the preferred way of
> form bean initialization and how can it access request parameters at
> initialization time?  I used a couple hidden form fields which the jsp
> page initialized to the corresponding request parameters, but this
> does not take effect until AFTER the constructor is called.  Any help
> would be much appreciated.
>

This type of question is really more suited for the STRUTS-USER list.

The design pattern I prefer is exemplified in the Struts Example
application -- use an Action to set up the form bean *before* the form is
displayed.

In the example app, note how an "Edit Subscription" transaction is
handled:

* The "/editSubscription" action is called to pre-fill the
  SubscriptionForm bean and place it in request scope.  If you
  need values from the request that triggered this, they are
  accessible to the Action and it can call setters as needed.

* This action forwards to "subscription.jsp" which shows the
  pre-filled-out form.

* The submit goes to the "/saveSubscription" action which
  updates the database (assuming successful validation).

> TIA!
> Jon
>
>

Craig McClanahan



--
To unsubscribe, e-mail:   <mailto:struts-dev-unsubscribe@jakarta.apache.org
>
For additional commands, e-mail: <mailto:struts-dev-help@jakarta.apache.org
>





--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>