Class RequiredValidator

  • All Implemented Interfaces:
    Validator

    public class RequiredValidator
    extends Object
    implements Validator

    A specialized Validator implementation that checks whether data was entered in mandatory fields.

    This validator implementation can be assigned to mandatory input fields of different types. When the form is validated it checks if the field contains data. The isValid() method is implemented in a generic way supporting multiple data types. Thus this validator can collaborate with input fields of different types. An object passed to the isValid() method is rejected in any of the following cases:

    • the object is null
    • the object is a string that is empty or contains only whitespace
    • the object implements the Iterable interface, and the iteration does not contain any elements
    • the object is an array with no elements
    In any other case the object is accepted.

    RequiredValidator does not support any properties. The following table lists the error messages that are generated by this validator class:

    Message key Description Parameters
    "ERR_FIELD_REQUIRED" This input field must contain a value. This is the error code which will be returned when the passed in object is rejected. no parameters

    An instance of this class can be shared between multiple input components. If additional validations are to be performed for a mandatory input field, a ChainValidator can be used that is initialized with a RequiredValidator and arbitrary other validator implementations.

    Version:
    $Id: RequiredValidator.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Constructor Detail

      • RequiredValidator

        public RequiredValidator()
    • Method Detail

      • isValid

        public ValidationResult isValid​(Object o,
                                        TransformerContext ctx)
        Validates the passed in object. This implementation checks whether the object is defined.
        Specified by:
        isValid in interface Validator
        Parameters:
        o - the object to be validated
        ctx - the transformer context
        Returns:
        a result object indicating whether the object is valid