File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes validation in struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "validation in struts2" Watch "validation in struts2" New topic

validation in struts2

Rajani Panchumarthy

Joined: Jan 27, 2009
Posts: 22
1. When I try to validate the form parameters in struts2 for "required" validation, all the other messages that are associated with the field are also being displayed.For ex:
<field name="empid">
<field-validator type="required">
<message>Emp id is required</message>
<field-validator type="regex">
<param name="expression"><![CDATA[^\d*$]]></param>
<message>Employee ID should be a number</message>
<field-validator type="regex">
<param name="expression">^[1-9]\d*$</param>
<message>Employee ID cannot begin with a zero</message>

In the above case, empid is an integer, when I do not specify any id and submit the form,all the above messages are displayed.How do I avoid this.

2.I ahve used the following pattern in validation.xml file for password and confrm password matching:
<validator type="expression">
<param name="expression">
password eq password2
<message>Confirm password should be same as password.</message>
But there is no effect when I submit the form with differenct passwords.

Also, tried to use the following in action class but in vain:
@FieldExpressionValidator(expression = "(password == password2)", fieldName = "password2", message = "Passwords must match.")
Could you someone help me for the above two scenarios.

Thanks in advance.
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

For 1, use Short Circuiting Validator.

The future is here. It's just not evenly distributed yet. - William Gibson
Sonny Gill LinkedIn
I agree. Here's the link:
subject: validation in struts2
It's not a secret anymore!