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
TheCompositeValidatorallows 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 modesANDandOR.- Since:
- 3.0
- Author:
- Vaadin Ltd.
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCompositeValidator.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.CombinationModeMODE_ANDDeprecated.As of 7.0, useCompositeValidator.CombinationMode.ANDinsteadstatic CompositeValidator.CombinationModeMODE_ORDeprecated.As of 7.0, useCompositeValidator.CombinationMode.ORinstead
-
Constructor Summary
Constructors Constructor Description CompositeValidator()Construct a composite validator inANDmode 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 voidaddValidator(Validator validator)Adds validator to the interface.StringgetErrorMessage()Gets the error message for the composite validator.CompositeValidator.CombinationModegetMode()Gets the mode of the validator.Collection<Validator>getSubValidators(Class validatorType)Gets sub-validators by class.voidremoveValidator(Validator validator)Removes a validator from the composite.voidsetErrorMessage(String errorMessage)Sets the message to be included in the exception in case the value does not validate.voidsetMode(CompositeValidator.CombinationMode mode)Sets the mode of the validator.voidvalidate(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.ANDinstead
-
MODE_OR
@Deprecated public static final CompositeValidator.CombinationMode MODE_OR
Deprecated.As of 7.0, useCompositeValidator.CombinationMode.ORinstead
-
-
Constructor Detail
-
CompositeValidator
public CompositeValidator()
Construct a composite validator inANDmode 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:
validatein 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.ANDorCompositeValidator.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
ANDmode composite validators.If the validator is in
ORmode 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.
-
-