Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conditional validation in Struts2

 
Aneesh Anvar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a field in my application. but for some users that field will be disabled..
but if the field is displaying, it should be validated.. and no validation if it is disabled.
I'm using Struts2 and XML validation..

Can anyone help me in solving this issue?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post in the right forum next time. You posted in SCJP, but this belongs in the Struts forum

What have you done so far to achieve this requirement ?
 
Aneesh Anvar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've a field, which is validated client side in validation.xml..
But i want to make a generic validation.xml so that only when a particular field(say username) is present, it should validate, otherwise no need to validate..
But the code for the validation always remains there, for the field..If its present it ll validate, otherwise no... but in validation.xml, it wont support any condition like
(if username)
{
code for validate..
}

need to find out a new solution
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Aneesh..

are you using struts Validation framework..??
can you show your validation.xml?

Regards,
Raza..
 
Aneesh Anvar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Raza,

i am using struts2 validators... Here goes my validation.xml. here the firstname should be given a condition, like only if firstname is present in the jsp page then the validation should happen, otherwise dont take the validation code... But the validation code for the firstname wll always be presernt in the validation.xml...
This is my requirement.. i think now you got a better understanding of the problem...

<validators>
<field name="firstName">
<field-validator type="requiredstring">
<message key="validator.requiredstring"/>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">1</param>
<param name="maxLength">30</param>
<message key="validator.stringlength"/>
</field-validator>
</field>
<validators>
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok you can have a validate method in your Action class ..Rather than a Action-Name-Validation.xml.

In validate method you can perform the conditional validation.

For validate method your action class should implement ValidationAware Interface..

I think this might help you..If you have any problem then let me know.

Regards,
Raza.
 
Aneesh Anvar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi raza,
thank you for the reply.. But the validate method will make it a server side validation. I need a perfect client side validation..
If i give it in validate method, then like that field( username), if any other field requires such kind of validation, then the code ll become messy...
If we ve any generic condtion in xml, thinhs ll be pretty easy
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi aneesh..

validation with xml file is also a server side validation.

But for Client side you need to add validate ='true ' on your Form.

You can populate value and then make it disable ...it will work with validation.xml.


 
Aneesh Anvar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi raza,
i added the validate="true " in the form...The rest you wrote "You can populate value and then make it disable ...it will work with validation.xml. "
i couldnt make out. Coould you please explain much more clear...
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean validation.xml does not perform partial validation. Whatever is present inside validation.xml will be called for the validation.
Either you make the field value present by giving a default value or use validate method..

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic