Package com.vaadin.data.validator
Class CompositeValidator
- java.lang.Object
-
- com.vaadin.data.validator.CompositeValidator
-
- All Implemented Interfaces:
Validator
,Serializable
public class CompositeValidator extends Object implements Validator
TheCompositeValidator
allows you to chain (compose) many validators to validate one field. The contained validators may be required to all validate the value to validate or it may be enough that one contained validator validates the value. This behaviour is controlled by the modesAND
andOR
.- Since:
- 3.0
- Author:
- Vaadin Ltd.
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
CompositeValidator.CombinationMode
-
Nested classes/interfaces inherited from interface com.vaadin.data.Validator
Validator.EmptyValueException, Validator.InvalidValueException
-
-
Field Summary
Fields Modifier and Type Field Description static CompositeValidator.CombinationMode
MODE_AND
Deprecated.As of 7.0, useCompositeValidator.CombinationMode.AND
insteadstatic CompositeValidator.CombinationMode
MODE_OR
Deprecated.As of 7.0, useCompositeValidator.CombinationMode.OR
instead
-
Constructor Summary
Constructors Constructor Description CompositeValidator()
Construct a composite validator inAND
mode without error message.CompositeValidator(CompositeValidator.CombinationMode mode, String errorMessage)
Constructs a composite validator in given mode.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addValidator(Validator validator)
Adds validator to the interface.String
getErrorMessage()
Gets the error message for the composite validator.CompositeValidator.CombinationMode
getMode()
Gets the mode of the validator.Collection<Validator>
getSubValidators(Class validatorType)
Gets sub-validators by class.void
removeValidator(Validator validator)
Removes a validator from the composite.void
setErrorMessage(String errorMessage)
Sets the message to be included in the exception in case the value does not validate.void
setMode(CompositeValidator.CombinationMode mode)
Sets the mode of the validator.void
validate(Object value)
Validates the given value.
-
-
-
Field Detail
-
MODE_AND
@Deprecated public static final CompositeValidator.CombinationMode MODE_AND
Deprecated.As of 7.0, useCompositeValidator.CombinationMode.AND
instead
-
MODE_OR
@Deprecated public static final CompositeValidator.CombinationMode MODE_OR
Deprecated.As of 7.0, useCompositeValidator.CombinationMode.OR
instead
-
-
Constructor Detail
-
CompositeValidator
public CompositeValidator()
Construct a composite validator inAND
mode without error message.
-
CompositeValidator
public CompositeValidator(CompositeValidator.CombinationMode mode, String errorMessage)
Constructs a composite validator in given mode.- Parameters:
mode
-errorMessage
-
-
-
Method Detail
-
validate
public void validate(Object value) throws Validator.InvalidValueException
Validates the given value.The value is valid, if:
MODE_AND
: All of the sub-validators are validMODE_OR
: Any of the sub-validators are valid
- Specified by:
validate
in interfaceValidator
- Parameters:
value
- the value to check.- Throws:
Validator.InvalidValueException
- if the value is not valid.
-
getMode
public final CompositeValidator.CombinationMode getMode()
Gets the mode of the validator.- Returns:
- Operation mode of the validator:
CompositeValidator.CombinationMode.AND
orCompositeValidator.CombinationMode.OR
.
-
setMode
public void setMode(CompositeValidator.CombinationMode mode)
Sets the mode of the validator. The valid modes are:- Parameters:
mode
- the mode to set.
-
getErrorMessage
public String getErrorMessage()
Gets the error message for the composite validator. If the error message is null, original error messages of the sub-validators are used instead.
-
addValidator
public void addValidator(Validator validator)
Adds validator to the interface.- Parameters:
validator
- the Validator object which performs validation checks on this set of data field values.
-
removeValidator
public void removeValidator(Validator validator)
Removes a validator from the composite.- Parameters:
validator
- the Validator object which performs validation checks on this set of data field values.
-
getSubValidators
public Collection<Validator> getSubValidators(Class validatorType)
Gets sub-validators by class.If the component contains directly or recursively (it contains another composite containing the validator) validators compatible with given type they are returned. This only applies to
AND
mode composite validators.If the validator is in
OR
mode or does not contain any validators of given type null is returned.- Parameters:
validatorType
- The type of validators to return- Returns:
- Collection
of validators compatible with given type that must apply or null if none found.
-
setErrorMessage
public void setErrorMessage(String errorMessage)
Sets the message to be included in the exception in case the value does not validate. The exception message is typically shown to the end user.- Parameters:
errorMessage
- the error message.
-
-