Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Struts and the fly likes Validations using ActionErros Validate 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 "Validations using ActionErros Validate" Watch "Validations using ActionErros Validate" New topic
Author

Validations using ActionErros Validate

Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Hi,
I am using validations using ActionErrors for validations of my forms. In the struts-config.xml i have the validate=true and my form-bean has validations for a string field say 'name' for non-null and zero-length. It goes something like this....



My problem is, this validation is happening even before the action is loaded... I mean i always get the error being displayed.


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
andres matus
Greenhorn

Joined: Dec 21, 2007
Posts: 15
that's because the validate method of the form is called every time you instance the form, and that occurs when a page is requested. So any time you are accessing a page you are going to get the validation (and the reset). If you whant to use the validation that way shouldn't validate for null or set the propertie to some value by defualt.

Other way is to use the validator framework provided with struts.


Be free...do what ever you whant
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I am not sure that the previous response is 100% accurate. The validate method will be executed anytime an action is executed that has the validate attribute set to true (true is the default value). If you have an action that displays the pages, you probably want to turn off validation for that action. If you cannot figure it out, post your action mappings.

- Brent
andres matus
Greenhorn

Joined: Dec 21, 2007
Posts: 15
i'm pretty sure that if you comment your validate method the problem is solved. Now the issue is what do you want to validate and when. The best practice is to use validator framework, your action form should extend ValidatorForm and the super class will handle the validation with the default validators.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
I also understand it like you Brent.

"The validate method will be executed anytime an action is executed that has the validate attribute set to true."

But then i can not turn the validate attribute to false, because then the whole purpose of the validation is lost.


Even commenting the validate method as Andres said, would solve the problem. But the problem is not solved, it is vanished. I want to have validations and they must happen, but at the time I want them to. I have not used ValidatorForm earlier, and one thing that is rolling in my head is my form-bean already extends ActionForm, how do I make it to extend one more class "ValidatorForm" or so. Anyways coming back to what i am doing... here is all.

I have one main JSP, let us call primary.jsp which contains a drop-dwon and submit button. On clicking submit
1. I go to the action "someaction"--> work around backend database ---> and get some records. which i must show at the same primary.jsp.

2. These records are displayed using "bean:write" and have a "edit" link whose href attribute is set to the same action. On clicking "edit" i am simply transfering the record data and displaying it in text-box on "secondary.jsp"

Now, I want to keep validations for this "secondary.jsp". and I fixed it as I said earlier. The problem occured when i clicked the submit button on the first jsp-"primary.jsp". It as expected, did not even go to the database.
andres matus
Greenhorn

Joined: Dec 21, 2007
Posts: 15
ok...now you'r getting to my point. I wasnt saying that you have to comment the validator method, it was just to prove i'm right. Although the validator method is meant to be called when you submit a form, when you use it on a jsp page, it always called the validation and reset method, always!!, even if it's the first time you access it, just make a debug and you will see it. you'r options are, set the propertie with a default value (i.e. "") or just simply use validator framework, thats the best practice you can do. If you decide to do it, your form dont have to extend two clases, because it will no longer extend ActionForm, it will just extend ValidatorForm, that is a subclass of the struts form class. That form extending ValidatorForm dont need the validator method or the reset method, because uses the method of the super class. So you just need to get into the validator framework and you should be able to solve your problem.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
"I can not turn the validate attribute to false, because then the whole purpose of the validation is lost"

I guess I don't know what exactly you are trying to accomplish. I often have several actions that use the same form and only the one that is called when the user presses the "Save" button has validation turn on. For example, the action that displays a page should not have validation turned on. Are you using DispatchAction (or something like that)? That is one reason that I never use DispatchAction because it does not seem like you can configure validation for the various methods.

andres: if you leave off the validate attribute from your action mapping or give it a value of "true" then the validate method will be called. If you specify validate="false" then the validate method will not be called. The validation framework works because the ValidatorForm implements the validate method and applies the defined rules.

- Brent
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Brent i have put down what I am doing in my previous post. Coming back to your statement of mapping more than one action on single form, i sometimes wonder on what to do with the un-used variables? because not all the action may use the same-all form variables, the variable-set may differ from action-to-action. One action, say Action1 may use say 4 variables, other say Action2 may use 7 variables with 3 overlapped to Action1. Mapping these two action on single form would have 4+4=8 variables as whole set. But neither of the two action use the whole 8 variables... do we simply carry unused variables as hidden fields? I have not used DispatchAction ever.
Andres, i could set the field to "" but again the condition (field.length() <1) gets-stuck for validation. I am yet to plug a ValidatorForm.
[ January 07, 2008: Message edited by: Akhilesh Trivedi ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validations using ActionErros Validate
 
Similar Threads
Struts Validator Plug-In
use of ActionMessages in struts
component layout not wrapping inside Panel
Log4j in struts
where to put validation?