struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Albert Wong" <AW...@iProcessSolutions.com>
Subject RE: Setting focus on field with error
Date Thu, 16 Aug 2001 21:04:22 GMT
Struts' html:form tag takes in an optional "focus" property.  If
specified, Struts will generate the neccessary javascript to set the
focus on the specified element.

So, one possibility might be to add a focus attribute to your formbeans
which would allow you to do the following:

<html:form name="FORM_NAME" focus="<%= FORM_NAME.getFocus() %>"
type="com.myCompany.myApp.action.form.MyForm" method="POST"
action="MyAction.do">

Adding the focus property can still be considered consistent with the
responsibilty of the form bean -- to represent html form input, we're
just expanding it to include setting up that form too (in this case
which html input gets the focus on that particular form). 

Another way to look at it is, if the jsp page needs to know which form
input needs the focus, which object has that information?  In this case,
Actionform seems like the best candidate.  It knows which input faile
validation and therefore needs to have the focus set on it.

-----Original Message-----
From: Assenza, Chris [mailto:cassenza@Accessdc.com]
Sent: Thursday, August 16, 2001 1:21 PM
To: 'struts-user@jakarta.apache.org'
Subject: RE: Setting focus on field with error


I've just been asked to do the same exact thing in our app (what a
coincidence!!) but I haven't determined exactly what do yet.  In our
app,
ActionErrors are added by the name of the form field that had the error
(ie.
field1 is the ActionErrors property and also the html:text property on
the
JSP). 

I'm thinking that we can expose the ActionErrors object to the front-end
and
use the messages HashMap or methods inherited from ActionMessages to
identify a property and then use JavaScript to set the focus on that
property.  This can probably be done pretty cleanly with a taglib.  

Again, this is just speculation on my part and I need to do a lot of
research into ActionErrors and Messages because I haven't dug into the
API
much for them, having had no need to until now. Let me know what you
find
out! :)

Chris

Christopher Assenza
Phone:	412.201.6026
Fax:     412.201.6060
Email:	cassenza@accessdc.com
ACCESSDATA
Moving Your Business from Point A to Point e.SM
http://www.accessdc.com/



-----Original Message-----
From: Troy Hart [mailto:thart@part.net]
Sent: Thursday, August 16, 2001 3:56 PM
To: struts-user@jakarta.apache.org
Subject: Re: Setting focus on field with error


I could be wrong but I can't think of anything that would be very
convenient. 
The only way I could imagine doing it (and I'm not sure it would even
work) 
would be to invoke a javascript function every time the page loads. You
could 
add a bean with the data needed by the javascript  to the request, from
your

action class. NOTE: this would require you to forgo the automatic form 
validation and instead trigger validation from your action class. This
is
the 
only way to get the bean I talked about into the request (as a request 
attribute that is).

Troy

On Thursday 16 August 2001 11:03 am, you wrote:
> Is their a nifty way to have the focus automagically go to the field
with
> the error, on an input form?  For example, if I have a form with an
> 'amount' field, and the user enters an invalid amount, causing my
Action
to
> return an appropriate error, keyed in the ActionErrors container with
the
> 'amount' property, is there a slick way to have the focus set to that
field
> on the form?  I know how to have the error message display next to the
> offending field, but having the focus go there also would be nice.
>
> Thanks!
>
> Steve Molitor
> smolitor@erac.com

Mime
View raw message