Friday, January 20, 2006

Struts validator plugin

People working with struts know that when you have a form, you can create a form bean that 'll automatically load the form data. in older projects, this class that is created must extend ActionForm and contains a method validate.
This is depreciated, a better way is to include the validator plugin.

The validator plugin works with a other class for the form data.
Now the class created with the form data must extend ValidatorForm, it also must implement Serializable.
This class doens't contain the method validate nay more, the validation is now done with the xml file.
This is included in the struts-config.xml files of that module like:
WEB-INF/examplemodule/struts-config.xml
   <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
      <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/examplemodule/validation.xml" />
      <set-property property="stopOnFirstError" value="true" />
   </plug-in>

The validation of the form in the "examplemodule" module can now be found in the file:
WEB-INF/examplemodule/validation.xml
Here is some example code in this file:
 <form-validation>
   <formset>
      <form name="submitForm">
         <field>
            Describe a field from the form
         </field>
      </form>
   </formset>
 </form-validation>

No comments: