aspose file tools*
The moose likes Struts and the fly likes Where is the best place to validate? XML, .validate() or JS? other? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Where is the best place to validate? XML, .validate() or JS? other?" Watch "Where is the best place to validate? XML, .validate() or JS? other?" New topic
Author

Where is the best place to validate? XML, .validate() or JS? other?

Rex Norm
Ranch Hand

Joined: Dec 11, 2010
Posts: 41
I've been struggling with this as a newbie...

should I validate input using the validate method? in XML using validator? via Javascript?

i'm trying to keep life simple for me, and I wonder what you guys think? has anyone done all 3 and can offer some wisdom as to what works best under which conditions?

Ideally, I'd like to go to one spot to validate and troubleshoot. As it is, I'm a little put off by the fact that Struts is not very easy to debug.

_R

Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

In struts 1 I kept some of my validation in the validate method but in struts 2 I kept a lot of the simple validation with annotations.

I dont know that you want to particularly validate all your fields with JS as you can or may expose some of your internal rules to the client.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8968
    
    9

I usually use XML validation unless the validation logic is fairly complex, then I override the validation method.
Many people prefer JavaScript validation as it saves a round-trip to the server. I'm not working on apps with high traffic or latency, so a round-trip is fine for my purposes. Lucas has a good point about exposing your validation logic to the client.
The "best" validation strategy is the one that fills your requirements. It may seem confusing when you are just starting out, but one day you'll be glad you have a flexible framework.


[How To Ask Questions On JavaRanch]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Validation should always occur on the server regardless of whether any client-side validation takes place. Client-side validation is easy to circumvent.

Client-side validation helps with the user experience (quick feedback, no server round-trip) but should never be relied upon. The server code should always pretend that client-side validation does not exist.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Where is the best place to validate? XML, .validate() or JS? other?