Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Info

The example code for this tutorial, form_xml_validation, is available for checkout at https://svngithub.com/apache.org/repos/asf/struts/sandbox/trunk/struts2examples/Image Removedstruts-examples

Introduction

In this tutorial we'll cover how to validate a user's input in form fields using Struts 2's XML validation methodology. In the Form Validation tutorial we discussed validating a user's input using the validate method in the Action class. Using a separate XML validation file gives you the ability to use validators built-in to the Struts 2 framework.

...

To enable the user to edit his information that is stored in the Person object, we have this form:

Image RemovedImage Added

When the user submits the form, we want to validate his entries into the form fields.

...

In the XML validation file (for this example that is EditAction-validation.xml), is this XML:

Code Block
xmlxmltitleXML Validator Required String


<!DOCTYPE validators PUBLIC
 "-//OpenSymphonyApache GroupStruts//XWork Validator 1.0.23//EN"
 "http://wwwstruts.opensymphonyapache.comorg/xworkdtds/xwork-validator-1.0.23.dtd">

<validators>
 <validator type="requiredstring">
 	<param name="fieldname">personBean.firstName</param>
 	<message>First name is required.</message>
 </validator>
</validators>

...

For example if the user doesn't enter a value in the first name form field and clicks on the Save Changes button, he will see the following.

Image RemovedImage Added

Validating An Email Address

...

Code Block
xml
xml
titleEmail Validator


 <validator type="requiredstring">
 	<param name="fieldname">personBean.email</param>
 	<message>Email address is required.</message>
 </validator>
 <validator type="email">
 	<param name="fieldname">personBean.email</param>
 	<message>Email address not valid.</message>
 </validator>

...

Code Block
xml
xml
titleREGEX Validator


<validator type="requiredstring">
 	<param name="fieldname">personBean.phoneNumber</param>
 	<message>Phone number is required.</message>
 </validator>
<validator type="regex">
	<param name="fieldname">personBean.phoneNumber</param>
	<param name="expressionregex"><![CDATA[\d{3}-\d{3}-\d{4}]]></param>
	<message>Phone number must be entered as 999-999-9999.</message>
</validator>

...

Code Block
xml
xml
titleFieldExpression Validator


<validator type="fieldexpression">
	<param name="fieldname">personBean.carModels</param>
	<param name="expression"><![CDATA[personBean.carModels.length > 0]]></param>
	<message>You must select at least one car model.</message>
</validator>

...

If the user did not check any of the check boxes, the Array returned by the getCarModels method will have a length value of 0. Since the complete OGNL expression will only evaluate to true if the length value is greater than 0, the validation fails. The user will see this.

Image RemovedImage Added

The fieldexpression validator is useful when doing conditional validation of a user's input. If the OGNL expression doesn't evaluate to true then the user's input won't be allowed.

...