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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddFailedValidator(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.voidconvertWith(Converter converter, String attribute)Registers converter for specified model attribute.voidconvertWith(Converter converter, String... attributes)Registers converter for specified model attributes.voiddateFormat(String pattern, String... attributes)Registers date converters (Date -> String -> java.sql.Date) for specified model attributes.voiddateFormat(DateFormat format, String... attributes)Registers date converters (Date -> String -> java.sql.Date) for specified model attributes.errors()Provides an instance ofErrorsobject, filled with error messages after validation.Provides an instance of localizedErrorsobject, filled with error messages after validation.Returns a value of an attribute.booleanisValid()Implementation should call {#link validate()} internally.voidremoveValidator(Validator validator)voidtimestampFormat(String pattern, String... attributes)Registers timestamp converters (Date -> String -> java.sql.Timestamp) for specified model attributes.voidtimestampFormat(DateFormat format, String... attributes)Registers timestamp converters (Date -> String -> java.sql.Timestamp) for specified model attributes.voidvalidate()Runs all registered validators and collects errors if any.voidvalidate(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:
getin 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:ValidatableThis 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:
addFailedValidatorin 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:ValidatableImplementation should call {#link validate()} internally.- Specified by:
isValidin interfaceValidatable- Returns:
- true if object is valid.
-
validate
public void validate()Runs all registered validators and collects errors if any.- Specified by:
validatein interfaceValidatable
-
validate
public void validate(boolean reset)Runs all registered validators and collects errors if any.- Specified by:
validatein interfaceValidatable- Parameters:
reset- true to reset all previous validation errors.
-
errors
Provides an instance ofErrorsobject, filled with error messages after validation.- Specified by:
errorsin interfaceValidatable- Returns:
- an instance of
Errorsobject, filled with error messages after validation.
-
errors
Provides an instance of localizedErrorsobject, filled with error messages after validation.- Specified by:
errorsin interfaceValidatable- Parameters:
locale- locale.- Returns:
- an instance of localized
Errorsobject, filled with error messages after validation.
-