• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts2 - Complex Validation

 
Scott Selikoff
author
Saloon Keeper
Posts: 4020
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4020
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic