There are two ways that the framework allows you to validate maps and collections.
You can represent validations by using explicit pseudo-properties. Maps use the key
and value
pseudo-properties while collections use the element
pseudo-property. An explicit usage where the value of the map entries must be a string no longer than 10 characters may be similar to the following example:
<bean-property name="map"> <property name="value"> <validator-instance class="StringTrimmingValidator"> <param name="maxLength" value="10"/> </validator-instance> </property> </bean-property>
You can also have containers of sub-beans. Note that in this example, you may need a class
or item-descriptor
tag in the inner bean-property
tag if introspection fails:
<bean-property name="addressMap"> <bean-property name="value" include-all-simple="true"> <property name="zipCode"> <validator-instance class="ZipCodeValidator"/> </property> </bean-property> </bean-property>
The implicit approach assumes nesting. For a map, the equivalent implicit XML may resemble:
<property name="map"> <validator-instance class="StringTrimmingValidator"> <param name="maxLength" value="10"/> </validator-instance> </property>
Note that when using the implicit containment XML, you cannot specify the key validator for maps, and you cannot override the default validator class. As such, it is recommended that you use the explicit form whenever possible.
The following container validators are available, whether using implicit or explicit pseudo-properties:
Container Class | Default Validator Class |
---|---|
|
|
|
|
Creating Custom Map Validators
The MapUpdatingValidator
can be extended using a MapUpdateHelper
, configuring it as the default MapUpdatingValidator
instance using a param
tag. You can do this using the class:
or nucleus:
prefix, depending on whether the Helper should be a new instance or one found from Nucleus.
The UnorderedCollectionUpdatingValidator
and its child, the ListUpdatingValidator
, support a CollectionUpdateHelper
. These helpers contain methods for creating a target container, finding a corresponding target item or creating a target item. This allows you to update a target repository item or bean container that requires the use of Nucleus-based manager or if you need custom logic to find an existing corresponding item.