dipsy
dipsy

Reputation:

Struts 2 validation

Can anyone please let me know how to check if start date > end date in Struts 2 validation.xml

Upvotes: 1

Views: 2045

Answers (4)

James Young
James Young

Reputation: 1432

If your using java.util.Date objects, this has worked for me.

    <validator type="expression">
        <param name="expression"><![CDATA[
                startDate.time < endDate.time
        ]]></param>
        <message>Start Date must be after End Date</message>
</validator>

Upvotes: 3

user186296
user186296

Reputation:

Do you know Hibernate Validator Framework? This is a great validation framework, based on annotations.

And there is a Plugin for Struts2:

Full hibernate Plugin: http://cwiki.apache.org/S2PLUGINS/full-hibernate-plugin.html

Upvotes: 0

yalestar
yalestar

Reputation: 9564

Since you're performing validation on two fields, one approach is to create a custom validator that operates on an entire object (e.g. User) that contains start and end date, as opposed to validating the fields independently. Then you could just do some simple date-comparison arithmetic in there.

The general term they use is non-field validation (or domain validation), which is covered here

There's a nice example in the source code for the Manning book Struts 2 in Action, chapter 10.

Upvotes: 0

Brian Yarger
Brian Yarger

Reputation: 1975

I think you can do this with an OGNL expression validator. Something like:

   <validator type="expression">
     <param name="expression">startDate.before(endDate)</param>
     <message>Start Date must be after End Date</message>
   </validator>

Upvotes: 1

Related Questions