Java Reference
In-Depth Information
errors.addGlobalError(
new SimpleError(username + " is already taken."));
}
if (!user.getPassword().equals(confirmPassword)) {
errors.addGlobalError(
new SimpleError("The passwords do not match."));
}
}
private UserDao userDao = MockUserDao.getInstance();
}
In the validation method, we make sure the username is not already
taken by someone else and that the password and confirm password
fields match.
As we can see, all fields are required. That will work fine as is, but with
six fields in total, a blank form will bombard the user with error mes-
sages. We can do something about that by hooking into the validation
process.
10.3
Dealing with a Bunch of Required Fields
Right now, there'll be as many error messages in the registration form
as there are missing fields. That can add up to a lot of error messages.
Instead, we can put one general error message at the top of the form
and just highlight the missing fields, as illustrated in Figure 10.4 , on
the next page.
Doing this is surprisingly easy. First, the action bean implements Val-
idationErrorHandler so that its handleValidationErrors ( ) method is called at
the end of the validation process. Next, that method checks for the pres-
ence of any field errors, which can be only required-field errors in this
case. If there's at least one field error, the method adds a global error:
Download email_19/src/stripesbook/action/RegisterActionBean.java
public class RegisterActionBean extends BaseActionBean
implements ValidationErrorHandler
{
public Resolution handleValidationErrors(ValidationErrors errors){
if (errors.hasFieldErrors()) {
errors.addGlobalError(
new SimpleError("All fields are required."));
}
return null ;
}
}
 
 
 
Search WWH ::




Custom Search