Extending Validate and Custom Validation in the Validate Method of an Action

 < Day Day Up > 



There are two main types of form validation. The first type is per field—in other words, the validation takes into account only one field at a time. The Validator Framework excels with this type of form validation.

The other type of form validation is the relationship between two fields. For example, let's say that your product form has a price and a list price field. The rule is that the list price should never be less than the price. You can add this validation in the ActionForm's validate method, but you could argue that this type of validation is more of a business rule than true validation.

Another example comes to mind. Let's say that you need the end user to enter two passwords. One is the password itself, and the second is to ensure that users entered what they thought they did. To write this, you could choose to override the validate method of the ValidatorForm class.

The ValidatorForm is needed to integrate the Validator Framework to Struts. The ValidatorForm subclasses ActionForm and overrides the validate method to use the framework. You can take this a step further by overriding the validate method of ValidatorForm and extending the functionality of the ValidatorForm to provide your own custom validation by following these steps:

  1. Override the validate method.

  2. Call the superclass validate method.

  3. Save a reference to the errors (if any) that the ValidatorForm superclass produces.

  4. Perform additional error checking; add errors.

  5. Return the errors.

To add password support to our ActionForm, add the following code:

 import org.apache.struts.validator.ValidatorForm; public class InputFormAll extends ValidatorForm {      private String userName;      private String birthDate;      private String email;      private String creditCard;      private String firstName;      private String lastName;      private String middleName;      private String website;      private String password="";      private String passwordCheck="";      public ActionErrors validate(ActionMapping mapping,                                   HttpServletRequest request){           ActionErrors errors =                       super.validate(mapping, request);           if (!(password.equals(passwordCheck))){                errors.add(                   "password",                   new ActionError("errors.password.nomatch"));           }           return errors;      } 

You can use this approach anytime you want to add validation that the Validator Framework does not yet provide. Now this is one way to extend the Validator Framework without writing your own custom rules. However, you could just write your own validation rule; in the next section, we show you how.



 < Day Day Up > 



Professional Jakarta Struts
Professional Jakarta Struts (Programmer to Programmer)
ISBN: 0764544373
EAN: 2147483647
Year: 2003
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net