aspose file tools*
The moose likes Struts and the fly likes Controlling Struts Validation Dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Controlling Struts Validation Dynamically" Watch "Controlling Struts Validation Dynamically" New topic
Author

Controlling Struts Validation Dynamically

James Gordon
Ranch Hand

Joined: Aug 09, 2002
Posts: 106
Hi,
I have some fields on a form which "might" need to be validated. Eg: When radio button 1 is chose, then validate field 1, ignore field 2. When radio button 2 is chose, then validate field 2, ignore field 1.
Thus, this is a bit contradicting with the common validation which struts provide, ie: if field 1 validation flag is set to true, then validation would always take place.
This become worst as I am using the struts javascript validation - <html:javascript> tag, which would generate the javascript for validation during runtime.
So, is there anyway for me to enable field validation dynamically instead of hard-coding.
Perhaps, writing my own javascript for those field is the easiest way but then the internationalization problem would pop up because error message is not taken from the ApplicationResources.properties anymore.
Please advice.
Thanks in advance.
Ravi Veera
Ranch Hand

Joined: Jun 23, 2001
Posts: 127

Perhaps, writing my own javascript for those field is the easiest way but then the internationalization problem would pop up because error message is not taken from the ApplicationResources.properties anymore

What I am suggesting is a kludge but may work in your case. You could have the error message as a property in your Bean class and read it in in your JSP file within your javascript function like this.

A nicer alternative would be to put your own conditional Javascript validation in the validator-rules.xml
James Gordon
Ranch Hand

Joined: Aug 09, 2002
Posts: 106
Hi Ravi,
Thanks four your reply.
The method of storing error message to a javascript variable is fine but I can't really find a way to pass in the argument (arg0), unless it is hard-coded.
What is in my ApplicationResources is something like this (actually I use bean:message):
errors.required={0} is mandatory.
testmodule.label=Field 1
<bean:message key="errors.required" arg0={Should Pass In testmodule.label}/>
Unless I can store the <bean:message> value to a JSP variable, else I can't see anyway to pass in the value for arg0.
Any help would be greatly appreciated.
Thanks in advance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Controlling Struts Validation Dynamically