| > Home |
Extends Validator - inheritance, indexed validation |
![]() |
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:
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.
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>
<!-- |
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.
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>
|
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.validator-rules.xml for the extends validator OR
simply add the validation-extends-rule.xml file to the list of xml config files
for the ValidatorPlugIn in the struts-config.xml.properties file along the following lineserrors.extends=Validator 'extends' error for {0} - check log messages for details