You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Craig R. McClanahan" <Cr...@eng.sun.com> on 2001/02/01 01:37:04 UTC

Re: question on errors, how to handle

Mike Campbell wrote:

>  I want to be able to have a set of errors returned to an input page
> such that I can spit out an error of a particular TYPE next to a
> particular input, to indicate something in particular was wrong with
> this particular field.  I'm a bit lost on how to accomplish this.Take
> your typical login screen consisting of user name and password.  If
> the login fails, I want the screen to be redrawn with errors that
> pertain to the user name field next to the user name field, and the
> errors associated with the password next to the password field.
> (Ignoring formatting), if my login screen looked
> like:---------------------------------------------------------------Login:
> ___________________Password:
> _____________---------------------------------------------------------------...and
> I typed in "foo9" in user name and nothing in password and given some
> contrived validation rules which will be obvious from the example
> below, I want something like the following to be
> shown:---------------------------------------------------------------Login:
> ____foo9____* The user name must be over 8 characters long* The user
> name must only consist of alphabetic characters.Password:
> _________________* The password was left blank, it must be
> provided.---------------------------------------------------------------Figuring
> out all these conditions, constructing the ActionError class with the
> ApplicationResources.properties key and adding the ActionErrors to the
> request is pretty simple, but I'm lost on the "how to assign an error
> to a particular input field" concept.Is this covered in the
> example?Thanks. --
>

  Mike Campbell     Email: mike.campbell@s1.com
  S1 Corporation    voice: 678.421.4641
  Software Engineer fax: 678.421.4865
  R & D             web: http://www.s1.com/
  Black Knight
>

When you store an error in the ActionErrors collection, you have the
option to associate it with a particular field:

    ActionErrors errors = new ActionErrors();
    String username = formBean.getUsername();
    String password = formBean.getPassword();
    if ((username == null) || (username.length() < 1)
        errors.add(new ActionError("username",
            "error.username.required"));
    ... and so on ...

Although the example app does not do so, you can utilize a recently
added feature of the <html:errors> tag to place the messages where you
want:

    ... prompt and field for the "username" field ...
    <html:errors property="username"/>

    ... prompt and field for the "password" field ...
    <html:errors property="password"/>

Craig McClanahan