• 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

Struts 2: conditional validation, how to do that?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Vidya
[ August 08, 2008: Message edited by: Vidya Moorthy ]
 
Ranch Hand
Posts: 83
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also overwrite the ActionSupport class's validate method and create custom required validate methods to do this.

http://www.opensymphony.com/xwork/api/com/opensymphony/xwork/ActionSupport.html
[ August 11, 2008: Message edited by: Charles Z Martin ]
reply
    Bookmark Topic Watch Topic
  • New Topic