views:

518

answers:

4

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

A: 

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.

yalestar
A: 

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>
Brian Yarger
A: 

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

Yoshi
A: 

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>