aspose file tools*
The moose likes Struts and the fly likes Struts2 - Complex Validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 - Complex Validation" Watch "Struts2 - Complex Validation" New topic
Author

Struts2 - Complex Validation

Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

Is there any way to validate information across two fields in Struts2? For example, lets say I want to enforce the rule fieldX must be less than fieldY. Is there any way to add an XML rule that says this?


My Blog: Down Home Country Coding with Scott Selikoff
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You *can* do pretty much anything with the expression validator, although anything more complicated than simple comparisons etc. may be better off inside a validate() method. Note that manual and XML- or annotation-based validation can work in concert w/ a call to super.validate().

The validation documentation covers the bundled validators, btw.
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3716
    
    5

Can it be done in XML or do I need to create a custom validator class? I like all the validations I have in place via the XML, I just want to add the additional rule comparing two fields.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

...

Sure, you can do it in XML, as the fieldexpression docs I linked to previously demonstrates.

That said, using an action's validate() method doesn't require making a custom validator class; it requires calling super.validate(), writing a compare, and adding a field error message on failure.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Struts2 - Complex Validation