Struts Form Validation
Go Through
1.
Form Validation
2.
Validate() method in ActionForm class
3.
Problem with validate()
4.
[Link]
5.
Message Resource Bundle
6.
[Link]
7.
ValidatorForm class
8.
Configuring the Validator in [Link]
9.
Example
Volvo IT
Struts Form Validation
2
[Link] Validation
it is the process of ensuring that the data that
are entered fall within the accepted boundaries
of the application.
Checking data against a standard or requirement
Volvo IT
Struts Form Validation
3
2. validate() method:
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
return [Link](mapping, request);
}
Validate() is to verify that the input submitted by the user
are correct and valid.
It returns a ActionErrors object, which consists of number of
message keys that is used to look up the text from the
appropriate message resource file.
Volvo IT
Struts Form Validation
4
[Link] with validate():
For every field validation we have to write bunch of if-else
statements.
Much validation coding needed.
There are no global rules for checking Null, Minlength,
Integer, Email etc.
It is hard coded.
For every form, validate() method need to be written.
Volvo IT
Struts Form Validation
5
[Link]
It is called as global rules file where all the rules are actually
defined.
Ex:- validation for Email, Number, phoneNo etc.
Each <validator> element defines one validation rule.
Struts provides FieldChecks class as a validator that uses
the GenericValidator and have methods like
validateRequired(), validateDate(), validateCreditCard().
Sample of a [Link] is:
Volvo IT
Struts Form Validation
6
<form-validation>
<global>
<validator name="required"
classname="[Link]"
method="validateRequired"
methodParams="[Link],
[Link],
[Link],
[Link],
[Link],
[Link]"
msg="[Link]"/>
<validator name=>
</validator>
<!- More validators defined here -->
</global>
</form-validation>
[Link]
Volvo IT
Struts Form Validation
7
5. Message Resource Bundle
Next step: add the msg for [Link] to the Resource
Bundle.
[Link] ={0} is required.
If a validator fails, it passes back a message key and
replacement parameters to resource bundle.
Other examples are:
[Link]={0} is invalid.
[Link]={0} is not in the range {1} through {2}.
Volvo IT
Struts Form Validation
8
[Link]
Application Specific(one per application).
It associates rules from global rule file with individual fields of
ActionForm.
The xml consists of a <formset> block with multiple <form>
blocks, one for each form.
Each form element is given its own name. This should
correspond to the form bean name.
Each form element is composed of a number of field elements.
The field elements designate which validator(s) to use with the
depends attribute.
Volvo IT
Struts Form Validation
9
<form-validation>
<formset>
<form name="registerForm">
<field property="firstName"
depends="required,minlength">
<arg0 key="[Link]"/>
<arg1 name ="minlength" key="${var:minlength}"
resource="false"/>
<var>
<var-name>minlength</var-name>
<var-value>4</var-value>
</var>
</field>
<field property="lastName" depends="required">
<arg0 key="[Link]"/>
</field>
</form>
</formset>
</form-validation>
[Link]
Volvo IT
Struts Form Validation
10
<global> tag of [Link] :
The <global> can hold as many <constant>s. A <constant> is much like a
Java constant. Declare it once and use wherever needed.
<form-validation>
<global>
<constant>
<constant-name>nameMask</constant-name>
<constant-value>^[A-Za-z ]*$</constant-value>
</constant>
</global>
<formset>
<form name="registerForm">
<field property="firstName" depends=mask">
<arg0 key="[Link]"/>
<var>
<var-name>mask</var-name>
<var-value>${nameMask}</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
[Link]
Volvo IT
Struts Form Validation
11
7. ValidatorForm class:
How validation failure became Action Error?
Here we have to extend our form from ValidatorForm.
ValidatorForm is a subclass of ActionForm and implements
the validate() method.
It executes the rules using the two xml files and generates
ActionErrors using the Message Resources defined in the
[Link].
Volvo IT
Struts Form Validation
12
8. Configuring the Validator
Configuration can be done by PlugIn.
A PlugIn is simply a configuration wrapper for a module-specific resource .
PlugIns are configured in the [Link] file.
<plug-in className="[Link]">
<set-property property="pathnames
value="/WEB-INF/[Link],
/WEB-INF/[Link]"/>
</plug-in>
The ValidatorPlugIn is a class that implements the PlugIn interface. It has
an attribute called pathnames
Volvo IT
Struts Form Validation
13
Steps to use Validator in Struts:
1.
Create the application specific ActionForm by extending
the ValidatorForm.
2.
Add the corresponding <form> element with <field> subelement for every form field that needs validation.
3.
List the rules to execute in the <field>s depends
attribute.
4.
For every rule, add the error message with predefined
name to the message bundle.
5.
For every rule, supply the argNs keys to the resource
bundle.
Volvo IT
Struts Form Validation
14
[Link]:
[Link]
Volvo IT
Struts Form Validation
15
Any Questions ?
Volvo IT
Struts Form Validation
16