Validating selective fields of a JSP thru Struts Validator Frame


General J2EE: Validating selective fields of a JSP thru Struts Validator Frame

  1. Hi,

    I have a JSP in which I show or hide (using 'if' clause) few fields as per the parameters paased to it. By default (i.e. when no parameters are passed) couple of text fields are shown to the user with a submit button. To use Struts validation framework I have entered required details in the validation.xml file for all the fields that may be shown in my JSP. Now my problem is that whenever I submit the form the validator tries to validate ALL the field of the JSP (whereas the front-end HTML only display and has couple of fields, may be coz the validation.xml has enteries for all the fields) even when they may not have been show to the user.

    I have coded in this way just to wrap functionalities of one kind into one front end so that I dont have to code 5 seprate JSPs, 5 seprate forms and 5 action classes.

    My questions are: -

    1. Is it possible to do what I am trying to do (I am trying get the struts validation framework validate only those fields that are present in the HTML form shown to the user and leave the rest even when their respective enteries are there in the validation.xml file)? If it is possible then how?

    2. If it is NOT possible then wot is the best way to go about this problem?

    An early response is highly appreciated.
    Thanks in advance.

    Rasesh Kumar
  2. Since you have so much conditional logic in your form, you may want to add marker fields to the page and have the validation.xml file use the validWhen or validIf entries to see if it should even be looking at those fields.
  3. Reply to Rich[ Go to top ]

    Thanks for the reply Rich,

    I have already tried using validwhen byt the problem with validwhen is that it is not able to handle more than two fields in the 'and' or 'or' clause. And in my case I will have to compare too many fields (more than 2 for all cases) to judge wether I shld validate a field or not. Let me know if U know of any other way.
    Rasesh Kumar
  4. Reply to Rich[ Go to top ]

    Defining custom validators to use might be your only viable option if you're intent on using the Validation framework.
  5. Can we do string comparision like below in <var-value> tag when using validwhen in struts.

    <var-value>( (name.equals("ravi")) or (*this* != null ) )</var-value>

    Please let me know as its not working for me.