Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Struts and the fly likes Validation in ActionForm or ActionClass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Validation in ActionForm or ActionClass " Watch "Validation in ActionForm or ActionClass " New topic

Validation in ActionForm or ActionClass

kumar shinde
Ranch Hand

Joined: Oct 17, 2005
Posts: 36
Friends ,

We can perform validations in ActionForm using validate() method (using ActionErrors ). We can also validate in Action class using ActionErrors .

My question is that for performing validations how do we decide that we have to validate from ActionForm or ActionClass . When should the validations be performed through ActionClass or ActionForm ?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Good question!

Here's my general rule of thumb: If it's a simple validation such as testing for existence, whether it's an integer, valid date format, etc., put it in the validate() method of the ActionForm. If it's something that could be described as a "business rule", it should be done in the action class, and the actual verification logic should go in a class that is part of the "model" layer of your MVC application.

For example, if a password input cannot be blank, that's a simple rule and can be done in the validate method. If a password has to have at least one upper-case character and one numeric character, but cannot start with a numeric character, that's a business rule, and should be done in the Action class with the rule itself coming from a model-layer class.

The point of making this distinction is to preserve the integrity of the MVC model.

Consultant, Sima Solutions
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I agree with Merrill's suggestion. To take his example a step further. Say you have to validate that the entered password does not match any of the user's previous 8 passwords. There is no way that you would want to implement this type of validation in the form.

- Brent
kumar shinde
Ranch Hand

Joined: Oct 17, 2005
Posts: 36
Thank You very much Merrill Higginson and Brent Sterling

Now I am able to make distinction about validations....
I agree. Here's the link:
subject: Validation in ActionForm or ActionClass
jQuery in Action, 3rd edition