Class ValidationSupport

java.lang.Object
org.javalite.validation.ValidationSupport
All Implemented Interfaces:
Validatable
Direct Known Subclasses:
JSONBase, ModelRegistry

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

    • ValidationSupport

      public ValidationSupport()
  • Method Details

    • 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.
    • validateWith

      public ValidationBuilder validateWith(Validator validator)
    • validateWith

      public ValidationBuilder validateWith(Validator... validators)
    • validateWith

      public ValidationBuilder validateWith(List<Validator> list)
    • validateNumericalityOf

      public NumericValidationBuilder validateNumericalityOf(String... attributes)
    • validatePresenceOf

      public ValidationBuilder validatePresenceOf(String... attributes)
    • removeValidator

      public void removeValidator(Validator validator)
    • validators

      public List<Validator> validators()
    • 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()
      Runs all registered validators and collects errors if any.
      Specified by:
      validate in interface Validatable
    • validate

      public void validate(boolean reset)
      Runs all registered validators and collects errors if any.
      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.