Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

custom validation

 
Stu Johns
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to pass a value to my own validation method. Basically i want to do the same thing as mask or minlength where you tell it a variable name and value. Anyone know how i can access a variable defined in this way?

thanks.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stu Johns wrote:I want to pass a value to my own validation method.

Which validation method you're using, is it ValidatorForm#validate() method OR Validator Plugin(validator.xml). In former case, you can pass the name=value parameters using 'request' object and accessing them "validate()" method, this is my wild guess and you may not get any success. In later case I don't think we can do this OR I don't know.

Meanwhile, why do you want to this complications ?

HTH.
 
Stu Johns
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to try and use the validation.xml approach.
It was purely from an educational point of view, I didnt know if it was possible or if the mask validation (and other struts inbuilts) was a special case.

Stu
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stu Johns wrote:It was purely from an educational point of view,

Then its OK, you can play around with Struts validator framework. Validation is predefined thing which ensures the data is coming the format we wanted, its not a runtime/user wished thing to do
 
Stu Johns
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may have misunderstood the question?
What i'm trying to do was pass a value using the <var-name> and <var-value> tags of <field> in validation-rules.xml
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stu Johns wrote:You may have misunderstood the question?

Then please elaborate, so that it make some sense?
Stu Johns wrote:What i'm trying to do was pass a value using the <var-name> and <var-value> tags of <field> in validation-rules.xml

Where do you want to "pass the value" ?
 
Stu Johns
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to use a value I set in the xml file in a method i am using to do the validation.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you writing a custom validator, or writing a validate() method in your action form? If the latter, then AFAIK you don't have access to anything in the validation config.

If the former, follow the example in the documentation.

http://struts.apache.org/1.2.9/userGuide/dev_validator.html

(You neglected to mention which version of Struts you're using.)
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm hard to do, but there are NO classes I saw in Struts API which can have an access to validatore.xml which returns the <field> element,t still have look at this method, may this methods put some light. OR I don't know much about your requirement
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not hard to do at all. The link I supplied shows precisely how to do it. It's not Struts, it's commons validator.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may be right David, but the thing is I didn't understand the authors requirement, and what I concluded finally was, the author want to get the <var-name> and <var-value> from validator.xml for the specific bean/form.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I thought, too, and that's what custom validators do, as does the example on the validation doc page.
 
Stu Johns
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
I've found what i was after in the link. The bit i needed was field.getVarValue
Thanks for your help guys (and patience)

Stu
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stu Johns wrote:Thanks for your help guys (and patience)

Major credits goes to David, I was just wondering here and there
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic