You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Molitor, Stephen" <SM...@erac.com> on 2001/08/16 19:03:32 UTC

Setting focus on field with error

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

Re: Setting focus on field with error

Posted by Troy Hart <th...@part.net>.
On Thursday 16 August 2001 01:55 pm, you wrote:
> 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).

oops, wasn't thinking straight. The validate method takes the request as an 
argument so you wouldn't need to forgo automatic form validation. 

>
> 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

Re: Setting focus on field with error

Posted by Troy Hart <th...@part.net>.
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