• 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

Struts 2: conditional validation, how to do that?

Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I am new to Struts 2. I have a form with 2 fields, last name and id. If somebody wants to search the data from the Database by either one of the field, they can enter only one field.I wrote a search-validation.xml with both the fields as required string. In the action class, i am checking if both of them are null, go to input page, otherwise, go to the success page.
On deployment, if I miss one of the fields, it still gives me a field is required message.
Is there anything I should do with the validation.xml file to fix this?
The other option I have is write two forms with 2 actions and 2 validation.xml files. Is that the best route?
P.S: I was checking struts validation and in struts 1.2, they have conditional validation. Could I use that or something like that in Struts 2
Thanks in advance
[ August 08, 2008: Message edited by: Vidya Moorthy ]
Ranch Hand
Posts: 83
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First Point :
Well if this is what you wrote in Action class

then you are using wrong operator in if condition, you are using a single & which is used for bitwise AND condition, which is not what you want.

You need to use boolean AND operator here which is ==> &&

Second Point

you will still have to write a custom validation for this type of scenario.
If you are using client side validation then you can do it using simple java ascript function

Let me know if you need help on JavaScript code....

Hope this helps....
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also overwrite the ActionSupport class's validate method and create custom required validate methods to do this.

[ August 11, 2008: Message edited by: Charles Z Martin ]
This tiny ad is guaranteed to be gluten free.
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic