Package org.javalite.validation
Class ValidationSupport
java.lang.Object
org.javalite.validation.ValidationSupport
- All Implemented Interfaces:
Validatable
- Direct Known Subclasses:
JSONBase
,ModelRegistry
Manages validators and converters.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addFailedValidator(Validator validator, String errorKey)
This method is not to add validators for future processing.<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.converterForValue(String attribute, Object value, Class<T> destinationClass)
Returns converter for specified model attribute, able to convert value to an instance of destinationClass.void
convertWith(Converter converter, String attribute)
Registers converter for specified model attribute.void
convertWith(Converter converter, String... attributes)
Registers converter for specified model attributes.void
dateFormat(String pattern, String... attributes)
Registers date converters (Date -> String -> java.sql.Date) for specified model attributes.void
dateFormat(DateFormat format, String... attributes)
Registers date converters (Date -> String -> java.sql.Date) for specified model attributes.errors()
Provides an instance ofErrors
object, filled with error messages after validation.Provides an instance of localizedErrors
object, filled with error messages after validation.Returns a value of an attribute.boolean
isValid()
Implementation should call {#link validate()} internally.void
removeValidator(Validator validator)
void
timestampFormat(String pattern, String... attributes)
Registers timestamp converters (Date -> String -> java.sql.Timestamp) for specified model attributes.void
timestampFormat(DateFormat format, String... attributes)
Registers timestamp converters (Date -> String -> java.sql.Timestamp) for specified model attributes.void
validate()
Runs all registered validators and collects errors if any.void
validate(boolean reset)
Runs all registered validators and collects errors if any.validateNumericalityOf(String... attributes)
validatePresenceOf(String... attributes)
validateWith(List<Validator> list)
validateWith(Validator validator)
validateWith(Validator... validators)
-
Constructor Details
-
ValidationSupport
public ValidationSupport()
-
-
Method Details
-
convertWith
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
-
validateWith
-
validateWith
-
validateNumericalityOf
-
validatePresenceOf
-
removeValidator
-
validators
-
dateFormat
Registers date converters (Date -> String -> java.sql.Date) for specified model attributes. -
dateFormat
Registers date converters (Date -> String -> java.sql.Date) for specified model attributes. -
timestampFormat
Registers timestamp converters (Date -> String -> java.sql.Timestamp) for specified model attributes. -
timestampFormat
Registers timestamp converters (Date -> String -> java.sql.Timestamp) for specified model attributes. -
convertWith
Registers converter for specified model attributes. -
get
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 interfaceValidatable
- 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
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 interfaceValidatable
- 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 interfaceValidatable
- Returns:
- true if object is valid.
-
validate
public void validate()Runs all registered validators and collects errors if any.- Specified by:
validate
in interfaceValidatable
-
validate
public void validate(boolean reset)Runs all registered validators and collects errors if any.- Specified by:
validate
in interfaceValidatable
- Parameters:
reset
- true to reset all previous validation errors.
-
errors
Provides an instance ofErrors
object, filled with error messages after validation.- Specified by:
errors
in interfaceValidatable
- Returns:
- an instance of
Errors
object, filled with error messages after validation.
-
errors
Provides an instance of localizedErrors
object, filled with error messages after validation.- Specified by:
errors
in interfaceValidatable
- Parameters:
locale
- locale.- Returns:
- an instance of localized
Errors
object, filled with error messages after validation.
-