com.opensymphony.xwork.validator.validators
Class CollectionFieldValidator
java.lang.Object
com.opensymphony.xwork.validator.validators.ValidatorSupport
com.opensymphony.xwork.validator.validators.FieldValidatorSupport
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 person
s 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
- - project across the collection of
person
objects
and make sure the conditions are valid (eg. name is more than 4 characters
- - filter out the result of previous projection that are not valid (eg. false)
- - check the size of the filtered collection, if its greater than zero,
we have some validation that fails
- property - the full property name that this validator should validate.
eg. if "persons" is a collection of Persons object with a property
called name the property would be "persons.name"
- validatorRef - validator name of an existing validator (could not be "collection" validator)
eg. required, requiredstring, int etc.
- validatorParams - the parameters to be passed into the validator referenced
by the "validatorParams" attribute above.
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
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 |
CollectionFieldValidator
public CollectionFieldValidator()
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 :-
- persons[0].name
- persons[1].name
- Parameters:
obj
- iterator
- result
- overallPropertyName
-
- Throws:
ognl.OgnlException
CloneNotSupportedException