Fine day to you Jasmine
frankly, some time ago I came across very same problem. As far as I am concerned using only provided validators
won't work. I'm going to state here how I solved the problem, as well as other possible (hypothetical) solutions.
I haven't used validator in form, you probably seek. The task requires branching and 2 different tests.
If I got it right something like this:
I used server-side validation, and validated input in the Action class.
Struts validator gives you opportunity to create your own validation classes
(
http://struts.apache.org/userGuide/dev_validator.html) .
I haven't tried but after writing your own class you could also add CDATA part to validator-rules.xml file, that
would create client-side validation Javascript. Than, it's up to you how you write the validation classes. I'm going
to write my own classes with such feature. As soon as I'll have something, I'm going to post it here.
Now for the other possible solutions.
First of all it is VERY important to note one phenomena:
Using strings for conditional statements are pain (ie: contact.country == 'en'). PLEASE NOTE that this is code used
to generate JAVASCRIPT!
In case of validwhen the generated code will look like this (obviously):
neither single nor double quotes will work.
Unfortunatelly, escaping characters or using character entities won't help either, since validator will add another
backslash resulting in
Does anybody know how to enforce single backslashes?
Following part is for those willing to investigate the problem.
My other ideas:
Idea 1 (Failed):
Use 2 validator tags for one field moving less probable option prior to more probable. If the first option would
fail, second validation would override first.
example 1:
result 1:
Struts will generate 2 validator functions. One function will succeed while other will fail, rendering an error (and
vice versa).
Idea 2 (failed):
Omit mask attribute and implement reg exp. validation in validWhen directly.
example 2:
result 2:
Failure, due to problem with above-described strings.(Using single quotes won't even generate the code) Validator will generate this code:
uff...
This way seems to be a dead end.
Martin
[ October 09, 2004: Message edited by: Martin Podrouzek ]