views:

378

answers:

0

In my Spring application, I have a form that has multiple inputs of the same field (the user can enter several user names at once). In the validator, I check for the existance of each user name and I want to show an error on the form next to each invalid user name. I can set an error in the validator that will show on the form, but this is a generic message and doesn't show which field is incorrect.

The command object is like this:

public class UserCommand {
  private int currentUserID = 0;
  private List<User> users= null;
  //...getters and setters for currentUserID and users
}

The User bean contains two values:

public class User {
  private String userName = null;
  private String firstName = null;
  //...getters and setters for userName and firstName
}

The validator iterates over the userNames List and checks if each one is in the database. If not, it raises an error. However, I can't set the error message for just that user name - I want to do something like:

errors.rejectValue("users[0].userName", "error.user-not-found");

but this throws an error for an invalid getter on the UserCommand.

On the jsp page, I have a table with rows like (the currentUserID is set via a hidden field):

<tr> 
  <td><form:input id="userName_0" path="users[0].userName" /></td>
  <td><form:input id="firstName_0" path="users[0].firstName" /></td>
  <form:errors path="users[0].userName" cssClass="error"> 
    <td><form:errors path="users[0].userName"/></td> **This is where I want the specific error message**
  </form:errors>
</tr> 

Can I show an error message for an item in the list of the form Command? How would I set it in the validator and check for it on the jsp?

Barring that, could I have a place holder in the error message that is populated with the user name - like in the messages.properties

"error.user-not-found={0} is an invalid user name"

and then populate the error message like:

errors.rejectValue("errors", user, "error.user-not-found");

Is that what the

Errors void reject(String errorCode, Object[] errorArgs, String defaultMessage) method is for?