• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Best practice for Validation?

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic