> Home

Extends Validator - inheritance, indexed validation

Holiday Accommodation in Croatia

What is the Extends Validator?

Its a custom validator (just like the standard struts ones such as date) that allows you to inherit another set of validation rules. It also handles a couple of the (IMO) shortcomings with the current indexed validation:

N.B. This is Server Side only - doesn't do javascript.

The rest of this page contains the following:


Inheritance Example

For example say you wanted two sets of validations - one for adding a customer and another for updating the customer - identical, except the add function has one additional property - customer number. Using the extends validator the add form could inherit from the update form as follows:


       <formset>

           <form name="AddCustomer">

               <field property="customerNumber" depends="required, integer">
                  <arg0 key="Customer Number" resource="false">
               </field>

               <field property="" depends="extends">
                  <var><var-name>extends<var-name><var-value>UpdateCustomer<var-value></var>
               </field>

           </form>

           <form name="UpdateCustomer">

               <field property="customerName" depends="required">
                  <arg0 key="Customer Name" resource="false">
               </field>

               <field property="address" depends="required">
                  <arg0 key="Address" resource="false">
               </field>

               <field property="telephone" depends="required, integer">
                  <arg0 key="Telephone Number" resource="false">
               </field>

           </form>

       </formset>

The extends example above will run all the UpdateCustomer form validations plus the Customer Number validations for the AddCustomer form. Note that property="" for the extends field indicates that the UpdateCustomer validations will be performed against the ActionForm, rather than against a property of the ActionForm.


Indexed Example

This example shows using the extends validator for indexed properties as an alternative to using the indexedListProperty attribute. One advantage of this is that all the Item's indexed properties will be validated and all errors shown - rather than just stopping at the first error.

A second feature is that rather than displaying the standard "[property] is required" error message which doesn't indicate which of the indexed values is in error, this example specifies an <arg> element which specifies a property in the bean to append to the message's arguments.

So in this example the item.errors.required message would be defined in the .properties file something like "Line {1} - {0} is required" with the lineNumber property specified in the <arg> being added to all of the messages' arguments.


       <formset>

           <form name="OrderForm">

               <field property="customerNumber" depends="required, integer">
                  <arg0 key="Customer Number" resource="false">
               </field>

               <!-- orderItems is either a Collection or Array -->
               <field property="orderItems" depends="extends">
                  <var><var-name>extends<var-name><var-value>Items<var-value></var>
                  <arg0 key="lineNumber">
               </field>

           </form>

           <form name="Items">

               <field property="productCode" depends="required">
                  <arg0 key="Product Code" resource="false">
                  <msg name="required" key="item.errors.required">
               </field>

               <field property="amount" depends="required,integer">
                  <arg0 key="Sale Price" resource="false">
                  <msg name="required" key="item.errors.required">
                  <msg name="integer" key="item.errors.integer">
               </field>

           </form>

       </formset>

For the extends validator to do indexed properties then you simply specify a property that is a Collection or Array - in this example thats the orderItems property.


Two Levels of Indexed Properties

A question recently came up on the list asking if more than one level of indexed proeperties could be handled by the validator. I haven't tried it but I believe that this extends validator will do two levels in the following way;


       <formset>

           <form name="PlanForm">

               <field property="plan.planNumber" depends="required,integer">
                  <arg0 key="Plan Number" resource="false">
               </field>

               <field property="plan.planDesc" depends="required">
                  <arg0 key="Plan Description" resource="false">
               </field>

               <field property="plan.phaseList" depends="extends">
                  <var><var-name>extends<var-name><var-value>Phase<var-value></var>
               </field>

           </form>

           <form name="Phase">

               <field property="phaseNumber" depends="required,integer">
                  <arg0 key="Phase Number" resource="false">
               </field>

               <field property="plan.planDesc" depends="required">
                  <arg0 key="Plan Description" resource="false">
               </field>

               <!-- Second Level of indexed validation -->
               <field property="procedureNumber" indexedListProperty="procedureList" depends="required,intRange">
                  <arg0 key="Procedure Number" resource="false">
               </field>

               <field property="procedureDesc" indexedListProperty="procedureList" depends="required">
                  <arg0 key="Procedure Description" resource="false">
               </field>

               <field property="units" indexedListProperty="procedureList" depends="required">
                  <arg0 key="Units" resource="false">
               </field>

           </form>

       </formset>

Get it

View the java for the Struts 1.2.x version here.

See sample validation-rules.xml.

*** DOWNLOAD *** Download Struts 1.2.x version here. *** DOWNLOAD ***

*** DOWNLOAD *** Download Struts 1.1 version here. *** DOWNLOAD ***

Confession I haven't actually tested the Struts 1.1 version :-( - just modified it to use the 1.1 methods and compiled it.



Install