Struts and ValidatorForm --> does it work with variable number of form elements?

1.1k views Asked by At

HI,

I have a form where I generate rows of input elements (using JavaScript) for users to enter their data. Each row contains data about a particular box that the user is entering (e.g. height, width, length, etc.). I've successfully gotten this form to work and I can read from the ActionForm into/out of an ArrayList object.

I'd like to validate these entries to catch invalid entries up front. (I'm on 1.3.10) I can't seem to find any documentation about using ValidatorForm with a dynamic number of form elements. That is, I never know how many rows of boxes will be entered, and I wonder if anyone has successfully used Validator to validate dynamic form elements?

Is this even possible? Or am I resigned to implement the .validate() method?

Is it possible to use the Validator for some elements and leave the rest to the .validate() method? has anyone done this, and if so, what are the pitfalls?

I'd appreciate your comments or even a pointer to a resource where I can read up.

1

There are 1 answers

3
Naved On

You can do this by making following steps.
1. Define a class (simple POJO) say DimensionBean; which contains the value of one row input elements (like height, width, length etc).
2. In your Form bean (child of ValidatorForm), define an array or list of DimensionBean. The name of the array is arrDimensionBeans.
3. In your validation.xml you can define it as follows

<field property="height" indexedListProperty="arrDimensionBeans" depends="validwhen">
      <arg0 key="yourform.height.label"/>
        <var>
          <var-name>test</var-name>
          <var-value>((arrDimensionBeans[].width == null) or (*this* != null))</var-value>
        </var>
      </field>

This will check height only if width is null or height is not null. You can change the var-value as per your requirement. Hope this will help you to solve your problem.