This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes struts 2: custom java validation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts 2: custom java validation?" Watch "struts 2: custom java validation?" New topic
Author

struts 2: custom java validation?

Chris Boldon
Ranch Hand

Joined: Aug 10, 2006
Posts: 190
Greetings,

I have recently started using Struts 2 and am starting to port some of my old Struts applications over.

One of my application has some complex validation code that makes database calls to see if values are already present, etc.

Is there a way to utilitze the Struts 2 validation framework to do this? Or do I have to do the old school way and forward to different results in an if loop based on certain results?

Thanks in advance.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'm not sure what you're looking for--you can write custom Java validators in S2, or use the validate() method. If you have complicated logic that needs to return to a page other than the INPUT result it'll require more work.
Chris Boldon
Ranch Hand

Joined: Aug 10, 2006
Posts: 190
I guess I am looking for a way to implement custom validation that will be inspected at the same time as annotation and xml-validator validation.

the validate() method runs at a different priority from my experience.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Then you'd implement a custom validator, much the same way as in S1. It might be more straight-forward to implement it in a validate() method, however.

As far as the different "priority", just like with S1, you can call super.validate() inside the validate() method, so that the DB-based validation is run immediately after the XML/annotation-based validation, which I'd assume is what you'd want anyway.
 
 
subject: struts 2: custom java validation?
 
Similar Threads
Struts 2 - How do I remove validation messages from fields?
Migration from struts1.1 to struts2.0
Struts Validation for non formdata
Struts 2 + hibernate + validation
Controlling Struts Validation Dynamically