Struts Form Validation

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Struts Form Validation

Go Through

1.

Form Validation

2.

Validate() method in ActionForm class

3.

Problem with validate()

4.

Validator-rules.xml

5.

Message Resource Bundle

6.

Validation.xml

7.

ValidatorForm class

8.

Configuring the Validator in struts-config.xml

9.

Example

Volvo IT
Struts Form Validation
2

1.Form 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 super.validate(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

3.Problem 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

4.validator-rules.xml
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 validator-rules.xml is:

Volvo IT
Struts Form Validation
6

<form-validation>
<global>
<validator name="required"
classname="org.apache.struts.validator.FieldChecks"
method="validateRequired"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
msg="errors.required"/>
<validator name=>

</validator>
<!- More validators defined here -->
</global>
</form-validation>
validator-rules.xml

Volvo IT
Struts Form Validation
7

5. Message Resource Bundle


Next step: add the msg for errors.required to the Resource
Bundle.
errors.required ={0} is required.
If a validator fails, it passes back a message key and
replacement parameters to resource bundle.
Other examples are:
errors.invalid={0} is invalid.
errors.range={0} is not in the range {1} through {2}.

Volvo IT
Struts Form Validation
8

6.validation.xml
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="label.firstName"/>
<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="label.lastName"/>
</field>
</form>
</formset>
</form-validation>
validation.xml
Volvo IT

Struts Form Validation


10

<global> tag of validation.xml :


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="label.firstName"/>
<var>
<var-name>mask</var-name>
<var-value>${nameMask}</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
validation.xml
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
struts-config.xml.

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 struts-config.xml file.
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames
value="/WEB-INF/validator-rules.xml,
/WEB-INF/validation.xml"/>
</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

9.Example:

http://localhost:8080/StrutsValidateForm/

Volvo IT
Struts Form Validation
15

Any Questions ?

Volvo IT
Struts Form Validation
16

You might also like