I have handled this in two different ways.

1. Previously I had a preprocess action which populated the collection and that was all it did. In the struts-config, I set the preprocessAction.do as the input page. That way if the validation failed, struts will execute the preprocessAction which will repopulate the list and redisplay the form in tact.

2. Resently, I have been using DWR (Ajax) project to load the select list when the page is rendered. I do this because my list is dynamic and can change based on selections that the user enters on the page.


Inactive hide details for [EMAIL PROTECTED][EMAIL PROTECTED]


          [EMAIL PROTECTED]

          09/07/2005 05:20 AM

          Please respond to
          "Struts Users Mailing List" <user@struts.apache.org>

To

user@struts.apache.org

cc


Subject

Prepopulating DynaValidatorForm and validating

Hi all!

Googling and seraching the archive did not solve my problem, so
I will have a try. In my struts-config.xml, I have defined a
DynaValidatorForm and an action:

<form-bean name="editForm"
 type="org.apache.struts.validator.DynaValidatorForm">
 <form-property name="projectList" type="java.util.Collection"/>
 <form-property name="version" type="java.lang.String" initial=""/>
</form-bean>

<action path="/processEdit"
 type="com.materna.buc.buildmanager.actions.ProcessEditAction"
 scope="request"
 name="editForm"
 validate="true"
 input="/WEB-INF/jsp/edit.jsp">
 ...
</action>

As you can see, this form holds a list of projects in a collection,
which has to be populated before the corresponding jsp-page is shown.
So I have an Action which gets the (dynamic, not static) list of
projects from a database and puts it into the editForm like this:

public ActionForward execute( ... ) {

 DynaValidatorForm dynaForm = (DynaValidatorForm) form;

 // get list of projects from database
 Collection projectList = getDatabaseHandler().getProjectList();

 // put project list into DynaValidatorForm
 dynaForm.set( "projectList", projectList );

Everything works fine after showing my jsp-page with a combobox
populated with the data from my projectList - the user can choose
one project.
But on the same page there is a html:text field:

<html:text name="editForm" property="version"
 value="1.0" size="7" />

which corresponds to my editForms "version"-property.
When the user submits the form to processEdit, the version
property has to be validated to see if it is not empty.
So in my validation.xml I do the following:

<form name="editForm">
 <field property="version" depends="required">
   <arg0 key="edit.version" />
 </field>
</form>

If the user submits an empty version and validation fails,
he will be send back to the jsp-page, but it will not show
because the project list is not present any more. That seems
clear because the DynaValidatorForm is reset when the user
submits. I tried following workarounds:

1. I replaced the DynaValidatorForm with my own version
overwriting the reset - method to do nothing. Additionaly
set the scope to "session". But my project list gets still
lost (wondering why?)

2. Trying to pass my project list from one form to the other
like this

<html:hidden name="editForm" property="projectList"
 value="projectList"

in all variations always had to fail because the project list
will be send as a string and therefore can not be converted
back to a collection.

I considered to put my project list into application scope, but
as it is dynamic, large and not the only (dynamic and large) list
to be shown on my jsp-page (I only used it as an example in order
not to complicate my question), I am not sure if it is the right
way.

And writing an ActionForm with own validation and reset methods
is a lot of work and would not use the benefits of a
DynaValidatorForm...

So if you know a trick, solution or workaround, please help!

Thanks

Peter


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


Reply via email to