Monday, 28 May 2012

Struts Validation Introduction

Validation:
Struts Framework provides the functionality to validate the form data. It can be use to validate the data on the users browser as well as on the server side. Struts Framework emits the java scripts and it can be used to validate the form data on the client browser. Server side validation of the form can be accomplished by sub classing your From Bean with DynaValidatorForm class.

Using Validator Framework

Validator uses the XML file to pickup the validation rules to be applied to an form. In XML validation requirements are defined applied to a form. In case we need special validation rules not provided by the validator framework, we can plug in our own custom validations into Validator.

The Validator Framework uses two XML configuration files validator-rules.xml and validation.xml. The validator-rules.xml defines the standard validation routines, these are reusable and used in validation.xml. to define the form specific validations. The validation.xml defines the validations applied to a form bean.

Example

<form name="logonForm">
<field property="username"
   depends="required">
   <arg key="logonForm.username"/>
  </field>
   <field property="password"
   depends="required,mask">
   <arg key="logonForm.password"/>
   <var>
  <var-name>mask</var-name>
  <var-value>^[0-9a-zA-Z]*$</var-value>
  </var>
  </field>
  </form>

No comments:

Post a Comment