You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Pritchard, Sean" <sp...@exchange.DAYTONOH.NCR.com> on 2002/02/21 15:40:52 UTC

form validation

I've run into a bit of a sticking point on form validation and I'm wondering
whether anyone else has encountered this.  I have an Action EditUserAction
that works with a UserForm to edit a user's data (e.g. first name, last
name. email, etc.).  In my Action, I check the request for a valid token.
If the token is not valid, I get the user's data from the database (via the
model layer) and populate the form with it.  I then forward control to a jsp
that displays the form.  If the token is valid, I update the user's data in
the database (again via the model layer) with the data in the form.  So I
use the same Action to initially populate the form and then to process the
submitted form.

The problem I'm running into, is that I want to begin using form validation.
So I created a validate method that ensures the email address is not null or
zero-length.  The problem is, that the error message is displayed the first
time the form is displayed (i.e. before the form is submitted).  It seems I
should only validate the form when it is submitted rather than the first
time it is displayed.  It is currently being validated before the user
submits it, so if the user has not previously submitted an email address,
when they first see the form, the error message "Email address is required"
appears.  

My current design would call for the form validation to be invoked only when
the token is valid.  But Struts doesn't seem to lend itself to validating a
token within a form (because the token validation methods are protected
instance methods of Action).  It feels a bit like I'm fighting the direction
the framework wants to go, which makes me think my design is flawed.  Any
suggestions?  Should I use two separate actions, one to populate the form
and one to process the submit?  That would allow me to set validation on one
and not the other.  Should I check for a valid token inside my validate()
method and only look for errors if the token is valid?  

Thanks in advance for your suggestions.
Sean

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


Re: form validation

Posted by Ted Husted <hu...@apache.org>.
Two ActionMappings, one with validate=false and the other with
validate=true. 

One Action class should be sufficient, though. 

-- Ted Husted, Husted dot Com, Fairport NY US
-- Developing Java Web Applications with Struts
-- Tel: +1 585 737-3463
-- Web: http://husted.com/about/services


"Pritchard, Sean" wrote:
> 
> I've run into a bit of a sticking point on form validation and I'm wondering
> whether anyone else has encountered this.  I have an Action EditUserAction
> that works with a UserForm to edit a user's data (e.g. first name, last
> name. email, etc.).  In my Action, I check the request for a valid token.
> If the token is not valid, I get the user's data from the database (via the
> model layer) and populate the form with it.  I then forward control to a jsp
> that displays the form.  If the token is valid, I update the user's data in
> the database (again via the model layer) with the data in the form.  So I
> use the same Action to initially populate the form and then to process the
> submitted form.
> 
> The problem I'm running into, is that I want to begin using form validation.
> So I created a validate method that ensures the email address is not null or
> zero-length.  The problem is, that the error message is displayed the first
> time the form is displayed (i.e. before the form is submitted).  It seems I
> should only validate the form when it is submitted rather than the first
> time it is displayed.  It is currently being validated before the user
> submits it, so if the user has not previously submitted an email address,
> when they first see the form, the error message "Email address is required"
> appears.
> 
> My current design would call for the form validation to be invoked only when
> the token is valid.  But Struts doesn't seem to lend itself to validating a
> token within a form (because the token validation methods are protected
> instance methods of Action).  It feels a bit like I'm fighting the direction
> the framework wants to go, which makes me think my design is flawed.  Any
> suggestions?  Should I use two separate actions, one to populate the form
> and one to process the submit?  That would allow me to set validation on one
> and not the other.  Should I check for a valid token inside my validate()
> method and only look for errors if the token is valid?
> 
> Thanks in advance for your suggestions.
> Sean
> 
> --
> 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>


Re: form validation

Posted by keithBacon <ke...@yahoo.com>.
There are various solutions.
I put a hidden variable on my form (& in the formBean) with the form name in
it.
If it's not there I know not to validate the form data.
Some people have 2 Actions & Action classes for this but I fear that would lead
to too much code duplication.
However some of my Action classes are getting a bit big & unwieldy.

--- "Pritchard, Sean" <sp...@exchange.DAYTONOH.NCR.com> wrote:
> I've run into a bit of a sticking point on form validation and I'm wondering
> whether anyone else has encountered this.  I have an Action EditUserAction
> that works with a UserForm to edit a user's data (e.g. first name, last
> name. email, etc.).  In my Action, I check the request for a valid token.
> If the token is not valid, I get the user's data from the database (via the
> model layer) and populate the form with it.  I then forward control to a jsp
> that displays the form.  If the token is valid, I update the user's data in
> the database (again via the model layer) with the data in the form.  So I
> use the same Action to initially populate the form and then to process the
> submitted form.
> 
> The problem I'm running into, is that I want to begin using form validation.
> So I created a validate method that ensures the email address is not null or
> zero-length.  The problem is, that the error message is displayed the first
> time the form is displayed (i.e. before the form is submitted).  It seems I
> should only validate the form when it is submitted rather than the first
> time it is displayed.  It is currently being validated before the user
> submits it, so if the user has not previously submitted an email address,
> when they first see the form, the error message "Email address is required"
> appears.  
> 
> My current design would call for the form validation to be invoked only when
> the token is valid.  But Struts doesn't seem to lend itself to validating a
> token within a form (because the token validation methods are protected
> instance methods of Action).  It feels a bit like I'm fighting the direction
> the framework wants to go, which makes me think my design is flawed.  Any
> suggestions?  Should I use two separate actions, one to populate the form
> and one to process the submit?  That would allow me to set validation on one
> and not the other.  Should I check for a valid token inside my validate()
> method and only look for errors if the token is valid?  
> 
> Thanks in advance for your suggestions.
> Sean
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 


=====
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Search the archive:-
http://www.mail-archive.com/struts-user%40jakarta.apache.org/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Keith Bacon - Looking for struts work - South-East UK.
phone UK 07960 011275

__________________________________________________
Do You Yahoo!?
Yahoo! Sports - Coverage of the 2002 Olympic Games
http://sports.yahoo.com

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