• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Calling FieldCheck methods from validate() method

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am using Struts 1.2.9 and I have a question. We have a set of code that has had many different people working on it and everyone sadly does their own interpretation of how to validate with Struts.

Mainly we try to use the Struts validator by simply defining the validation rules in the validation.xml file. We have created a bunch of custom validation which we call from the validation.xml file and this works great.

In areas where we have felt that the validwhen would be too complicated to write in the xml file we have added custom validation in the validate() method in the Form class.

To do this we then usually write our own checks (length, type etc) with normal java code, example:

if (str.length() > <somevalue> {
errors.add(<someerror> ;
}

What I would like to do is to call the same method that the Struts framework calls (i.e. the ones defined in the org.apache.struts.validator.FieldChecks class) when it checks the validations defined in the xml file but I am not getting this to work as I want. The main thing is that I don't know how to get the "correct" arguments from my validate() method. There are a bunch of arguments that are sent to the validateXXX() methods in the FieldChecks class and I would like to be able to provide these as well, either that or be told how to do something to the effect I am hoping for.

Any help or pointers are welcome.

Thanks in advance,

Archie
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I'd suggest:

Go ahead and define the simple validations for the form in a validation.xml file and let the validation framework handle those. Then code the more complicated validations yourself in the validate method. If you call super.validate() in your validation code, you will be calling the Struts validation code. Once you have done this, you can then do your own validations. Example:

 
Archibald Zimonyi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,

thanks for the answer but sadly I don't think it's enough. I will show you why.

Assume we have a field called UserName that has an entry in validation.xml for maxlength. This length is set to 30.

My understanding is that (barring any overriding of validate() the xml validation is always run for a certain form. So whether I have a value in UserName or not the maxlength validation is run.

Now, UserName should only be checked for length if it is a monday. Writing this in the validation.xml is trickier then writing my own code in validate().

So the easy answer is to do is to override the validate() and implement:

if (monday) {
if (UserName.length > 30) {
errors.add(error);
}
}

Another way (if UserName would have been the only field to check for this Form) would be:

if (monday) {
super.validate();
}

However, I want to be able to validate just the UserName field if monday but still use the Struts framework to do it, i.e. using the Struts methods and error creation methods.

I hope that explains what I want. I know I can do my own code but the problem is I don't want to because it is too restrictive and I already have code written for the maxlength validation in Struts.

Thanks,

Archie
[ December 21, 2007: Message edited by: Archibald Zimonyi ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation. I believe I now understand your problem more clearly. However, I haven't really played around with the Struts validation code all that much, so won't be much help to you there.

I will say, though, that I fail to see that you're gaining much by using the Struts Validation Framework code instead of your own. It's quite simple code to write, and since you're writing custom code anyway, it would make the code easier to read and simpler if all the validations were right there in one place rather than making a lot of calls to Validation Framework code.

Good luck in finding the right solution.
 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!