Re: Prepopulating Form elements in ActionForms

2005-03-10 Thread Wendy Smoak
From: "Greg Pelly" <[EMAIL PROTECTED]>

> I am having trouble with the ActionForm reset() method. I am using the
> method to handle prepopulating form values.

The reset method gets called every time the form is populated from the
request params.  You typically only want to prepopulate a form once, just
before it is displayed for the first time.

Pre-population is usually done from the Action, not within the form itself.

It *is* necessary to set all checkboxes to false in reset, but that's not
prepopulating them, that's clearing them out so the framework can set them
properly from the contents of the request.

-- 
Wendy Smoak


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Prepopulating Form elements in ActionForms

2005-03-10 Thread Greg Pelly
I am having trouble with the ActionForm reset() method. I am using the
method to handle prepopulating form values.  My Struts app uses the Struts
Validator's DynaValidatorForm (which extends ActionForm via the
DynaActionForm class).  I am attempting the following behavior in a class I
call "BaseActionForm":

1. Prepopulate form values in the reset() method of BaseActionForm.
2. When the JSP is submitted, if no value has been populated for a variable
named "ptn" (and if other criteria are true), return to the original JSP
(via an action-mapping) and display an error message, plus show the form
inputs the user entered (not the prepopulated defaults).

The problem I run into is that the reset() method seems to be populating my
DynaValidatorForm before the page is loaded and after as well.  

I want the reset() method called beforehand - this prepopulates my form
values.  However, when DynaValidatorForm.reset() is called after the form is
submitted, it is "re"-prepopulating the submitted form with defaults, the
result being that if the user erases a prepopulated value, the ActionForm
repopulates it with the default.  In other words, a value of "foo" was
prepopulated, the user erases that value and submits, the form prepopulates
it again with "foo" (bad!) and passes control to the action.

I also want to make sure that if the form elements have been incorrectly
populated, when the form returns to the JSP, it has not clobbered the
user-supplied (albeit incorrect) empty form values.

I have tried adding a check in the ActionForm (if super.get("ptn") is ""),
but it seems that this check always returns true from within the
ActionForm's reset() method.  It appears that the ActionServlet populates
the form after it calls reset().

I have done the usual web searches but have not found any good resource that
explains the proper way to use reset() or handle form prepopulation using
Struts.  I am aware that the reset() method is primarily intended for
checkbox prepopulation (only), but I do not know of a better way to handle
the above issue.  I used this approach because it was recommended in
Apress's Pro Jakarta Struts.

Can anyone provide advice or point me to some code examples/resources
(preferably using the DynaValidatorForm subclass of ActionForm, which
retrieves form element values using super.get(), as shown above)?

Thanks,
Greg

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]