• 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

Validations using ActionErros Validate

 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic