Class ValidationSupport

  • All Implemented Interfaces:
    Validatable

    public class ValidationSupport
    extends Object
    implements Validatable
    Manages validators and converters.
    • Constructor Detail

      • ValidationSupport

        public ValidationSupport()
    • Method Detail

      • convertWith

        public void convertWith​(Converter converter,
                                String attribute)
        Registers converter for specified model attribute.
      • converterForClass

        public <S,​T> Converter<S,​T> converterForClass​(String attribute,
                                                                  Class<S> sourceClass,
                                                                  Class<T> destinationClass)
        Returns converter for specified model attribute, able to convert from sourceClass to destinationClass. Returns null if no suitable converter was found.
      • converterForValue

        public <T> Converter<Object,​T> converterForValue​(String attribute,
                                                               Object value,
                                                               Class<T> destinationClass)
        Returns converter for specified model attribute, able to convert value to an instance of destinationClass. Returns null if no suitable converter was found.
      • removeValidator

        public void removeValidator​(Validator validator)
      • dateFormat

        public void dateFormat​(String pattern,
                               String... attributes)
        Registers date converters (Date -> String -> java.sql.Date) for specified model attributes.
      • dateFormat

        public void dateFormat​(DateFormat format,
                               String... attributes)
        Registers date converters (Date -> String -> java.sql.Date) for specified model attributes.
      • timestampFormat

        public void timestampFormat​(String pattern,
                                    String... attributes)
        Registers timestamp converters (Date -> String -> java.sql.Timestamp) for specified model attributes.
      • timestampFormat

        public void timestampFormat​(DateFormat format,
                                    String... attributes)
        Registers timestamp converters (Date -> String -> java.sql.Timestamp) for specified model attributes.
      • convertWith

        public void convertWith​(Converter converter,
                                String... attributes)
        Registers converter for specified model attributes.
      • get

        public Object get​(String attributeName)
        Returns a value of an attribute. The current implementation uses reflection to get to a private or public attribute. Subclasses may override this behavior however they like.
        Specified by:
        get in interface Validatable
        Parameters:
        attributeName - name of attribute. For a standard class it would be an actual name of a field retrievable by reflection.
        Returns:
        value of attribute.
      • addFailedValidator

        public void addFailedValidator​(Validator validator,
                                       String errorKey)
        Description copied from interface: Validatable
        This method is not to add validators for future processing. This is instead used to add validators and their respective error messages in case those validators fail.
        Specified by:
        addFailedValidator in interface Validatable
        Parameters:
        validator - validator that failed validation (later to be used to retrieve error message)
        errorKey - - generally an attribute name that failed validation
      • isValid

        public boolean isValid()
        Description copied from interface: Validatable
        Implementation should call {#link validate()} internally.
        Specified by:
        isValid in interface Validatable
        Returns:
        true if object is valid.
      • validate

        public void validate()
        Description copied from interface: Validatable
        Runs validation. Will blow away any previous validation errors.
        Specified by:
        validate in interface Validatable
      • validate

        public void validate​(boolean reset)
        Description copied from interface: Validatable
        Runs validation.
        Specified by:
        validate in interface Validatable
        Parameters:
        reset - true to reset all previous validation errors.
      • errors

        public Errors errors()
        Provides an instance of Errors object, filled with error messages after validation.
        Specified by:
        errors in interface Validatable
        Returns:
        an instance of Errors object, filled with error messages after validation.
      • errors

        public Errors errors​(Locale locale)
        Provides an instance of localized Errors object, filled with error messages after validation.
        Specified by:
        errors in interface Validatable
        Parameters:
        locale - locale.
        Returns:
        an instance of localized Errors object, filled with error messages after validation.