Chapter 5: Form Validation


In this chapter:

  1. You will learn how to use Commons Validator with Struts via ValidatorForm and ValidatorActionForm

  2. You will learn about DynaActionForm and DynaActionValidatorForm

Validation is a beast that needs to be addressed at various levels using different strategies for different complexity levels in the validation itself.

  1. It is commonly accepted principle that the user input validation should happen as close to the presentation tier as possible. If there are only a bunch of HTML forms with trivial checks and the validation is limited to the UI, you can afford to implement validations using JavaScript.

  2. Getting too close with JavaScript is one extreme and is not pragmatic in everyday projects. On the other hand, postponing the validation until it manifests as a business logic exception, runtime exception or a database exception is unacceptable too. Another option is to programmatically validate the HTML Form data using helper classes in the web tier or code the validation right in the validate() method of the ActionForm itself ‚ which is what we did in Chapter 3.

  3. The third option is to externalize the validation into a XML file that confirms to the Commons Validator syntax and integrate it into Struts. This approach works very well for trivial checks, which is a case in approximately 50% of the projects. Examples of trivial checks are: Null Checks - Checking if a field is null, Number Check ‚ checking if the field value is numeric or not, Range Check ‚ checking if a numeric values lies within a range. These validations depend just on the fields being validated and nothing else.

Validator is a part of the Jakarta Commons project and depends on the following Commons Projects - BeanUtils , Logging , Collections , Digester and also on Jakarta ORO library. All of these are shipped with the Struts 1.1. You can find them in the lib directory of the Struts distribution.

Struts is bundled with Commons Validator 1.0. Commons Validator 1.1.1 has support for validating interdependent fields. It can be downloaded from the Jakarta Commons website and used instead of the validator bundled with Struts.




Struts Survival Guide. Basics to Best Practices
Struts Survival Guide: Basics to Best Practices (J2ee Survival Series)
ISBN: 0974848808
EAN: 2147483647
Year: 2004
Pages: 96

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