com.opensymphony.xwork.validator.validators
Class CollectionFieldValidator

java.lang.Object
  extended by com.opensymphony.xwork.validator.validators.ValidatorSupport
      extended by com.opensymphony.xwork.validator.validators.FieldValidatorSupport
          extended by com.opensymphony.xwork.validator.validators.CollectionFieldValidator
All Implemented Interfaces:
FieldValidator, ShortCircuitableValidator, Validator

public class CollectionFieldValidator
extends FieldValidatorSupport

Validate a property available in the object of a collection. This validator will not work when the 'nested' validator is an 'fieldexpression' or 'expression' validator.. If we want to use 'fieldexpression' or 'expression' validator we might want to use eg. the following expression for validation collections (using OGNL's projection and filtering capabilities.

For example if we have a collection of persons objects with 'name' properties we might want to consider the following OGNL expression.

 persons.{#this.name.length() > 4}.{? #this == false }.size() <= 0 
 
The idea is
 
 
 public class MyAction extends ActionSupport {
        private List persons = new ArrayList();
     ....
     public List getPersons() { return this.persons; }
     public void setPersons(List persons) { this.persons = persons; } 
 }
 
 
 public class Person {
    private String name;
    private Integer age;
    
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    
    public Integer getAge() { return age; }
    public void setAge(Integer age) { this.age = age; }
 }
 
 
 <validators>
    <field name="persons">
        <field-validator type="collection">
                        <param name="property">persons.name</param>
                        <param name="validatorRef">requiredstring</param>
             <param name="validatorParams['defaultMessage']">Must be String</param>
             <param name="validatorParams['trim']">true</param>
             <message> ... </message>
        </field-validator>
        <field-validator type="collection">
            <param name="property">persons.age</param>
            <param name="validatorRef">required</param>
            <param name="validatorParams['defaultMessage']">Must be filled in</param>
            <message> ... </message>
        </field-validator>
        <field-validator type="collection">
                        <param name="property">persons.age</param>
             <param name="validatorRef">int</param>
             <param name="validatorParams['defaultMessage']">Needs to be an integer</param>
             <message> ... </message>
        </field-validator>
    </field>
 </validators>
 
 
 

Version:
$Date$ $Id$
Author:
tmjee

Nested Class Summary
protected  class CollectionFieldValidator.CloneableIterator
          A cloneable iterator, when the iterator is cloned, its state is preserved, eg.
protected  class CollectionFieldValidator.PropertySpliter
          Splits up the properties supplied, eg.
 
Field Summary
 
Fields inherited from class com.opensymphony.xwork.validator.validators.ValidatorSupport
defaultMessage, log, messageKey
 
Constructor Summary
CollectionFieldValidator()
           
 
Method Summary
 String getProperty()
           
 Map getValidatorParams()
           
 String getValidatorRef()
           
protected  void populateValue(Object obj, CollectionFieldValidator.CloneableIterator iterator, List result, String overallPropertyName)
          Populate result with a list of property name, eg.
 void setProperty(String collection)
           
 void setValidatorParams(Map validatorParams)
           
 void setValidatorRef(String validatorRef)
           
 void validate(Object object)
          Validate the object.
 
Methods inherited from class com.opensymphony.xwork.validator.validators.FieldValidatorSupport
getFieldName, getValidatorType, setFieldName, setValidatorType
 
Methods inherited from class com.opensymphony.xwork.validator.validators.ValidatorSupport
addActionError, addFieldError, conditionalParse, getDefaultMessage, getFieldValue, getMessage, getMessageKey, getMessageParameters, getParse, getValidatorContext, isShortCircuit, setDefaultMessage, setMessageKey, setMessageParameters, setParse, setShortCircuit, setValidatorContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.opensymphony.xwork.validator.Validator
getDefaultMessage, getMessage, getMessageKey, getMessageParameters, getValidatorContext, setDefaultMessage, setMessageKey, setMessageParameters, setValidatorContext
 

Constructor Detail

CollectionFieldValidator

public CollectionFieldValidator()
Method Detail

getProperty

public String getProperty()

setProperty

public void setProperty(String collection)

getValidatorRef

public String getValidatorRef()

setValidatorRef

public void setValidatorRef(String validatorRef)

setValidatorParams

public void setValidatorParams(Map validatorParams)

getValidatorParams

public Map getValidatorParams()

validate

public void validate(Object object)
              throws ValidationException
Validate the object.

Throws:
ValidationException
See Also:
Validator#validate(Object)}

populateValue

protected void populateValue(Object obj,
                             CollectionFieldValidator.CloneableIterator iterator,
                             List result,
                             String overallPropertyName)
                      throws ognl.OgnlException,
                             CloneNotSupportedException
Populate result with a list of property name, eg. if we have a list of Person object with a property called "name". If we have 2 persons in the list the result would be :-

Parameters:
obj -
iterator -
result -
overallPropertyName -
Throws:
ognl.OgnlException
CloneNotSupportedException

WebWork Project Page