You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by mech <me...@rz.fh-augsburg.de> on 2002/12/29 17:33:38 UTC

Difficulties with form bean validate() for pre-populated form beans

Hi,

I'm using Struts 1.1b and one part of my webapp is a registration page
for new users.
I have to say that this thing works already somehow, but not really as
I'd like to have it.

Currently I'm not using the validate() method in my form bean and I'm
doing all form validation in the action class.

This I do for one reason: I need to pre-populate my form bean!

So I don't link from my webpage to "newaccount.jsp" but instead I link
to "newaccount.do" which is partly pre-populating the form bean for my
view newaccount.jsp because I need some data from my DB to dynamically
generate a pull down menu for my registration page.

So my newaccount.do is loaded, populates the form bean from DB,
validates the form data (if there is any at this moment) and forwards to
newaccount.jsp. 
The view now generates the html form and the pull down menu according to
the bean data from DB and waits for the user to submit the data back to
newaccount.do.

This time newaccount.do finds values in the form bean, validates it and
in case of errors submits back to the view displaying the ActionErrors
(still being able to use the db data for the pull down menu) or if
nothing went wrong it generates a new user entry and continues to do
something else.


So far that works, but I wonder if I shouldn't use the validate() method
of the form bean for simple validation checks. E.g. the user must enter
an not-empty email address or whatever.

Here comes my problem after I copied the validation work from the
action's execute() to the form bean's validate(). 

Because I need to pre-populate the form before displaying the view
whenever the validate() method finds an error it returns to the "input"
page. 
But in order to generate the input view, I would need to pre-populate
the form. Unfortunately in case of errors the execute() method that
would usually pre-populate my bean is now never called...

If I set "input" in struts-config.xml to "newaccount.do" I only get an
endless loop, after submitting wrong form data.
But if I choose "newaccount.jsp" I will get jsp errors because the html
form can't be build because of the missing form data.

So what do else?

The only examples that I found in my books about Struts where validate()
is used are too simple and just consider the case that the input form
doesn't need any pre-populated form data and would just sit and wait for
data. 

So I hope that anyone could give me a hint how to handle this problem
and could tell me how to use validate() + pre-populated forms or maybe
there are other more reasonable ways to tackle this issue.

Thanks
Michael


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