Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best practice for Validation?

 
Nate Leech
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a form in a jsp that I validate against using Struts Validation Framework. So I make my own custom Validation Method that uses methods from my database class to access the database and validate against it, to check for things like if a record already exists or something along those lines.

Is it good practice to have custom validation methods make calls to a database? Or should this only happen in the Action Class, where I would add an ActionMessage and then findforward to the requesting page?

Any help is greatly appreciated!

-Nate
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your suspicions are correct. Even though you can do validation that accesses the database in custom validation methods, generally speaking it's better to do those types of things in an Action form. One of the main reasons for doing this is that in the Action form you have more flexibility around what to do if an error occurs.

Merrill
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I disagree with Merrill here.

My belief is that the ActionForm's validate method should only contain simple syntax validation to answer questions like, "Did the user enter a valid date in this textfield?"

What you are explaining about hitting the DB is business validation, which belongs in the Action's realm. The Action should be responsible for handling any business logic validation errors. The Action should hit a delegate and if any problems are found down the chain, an exception can be thrown that the Action can catch and create an ActionMessage for.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My previous post did not correctly state my position. My intention was to state that the best place to do this type of validation is in the Action CLASS not the Action Form.

So, I agree with Marc on this.

Merrill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic