• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Conditional validation in Struts2

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 247
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..

reply
    Bookmark Topic Watch Topic
  • New Topic