Package org.javalite.validation
Class ValidationSupport
- java.lang.Object
-
- org.javalite.validation.ValidationSupport
-
- All Implemented Interfaces:
Validatable
public class ValidationSupport extends Object implements Validatable
Manages validators and converters.
-
-
Constructor Summary
Constructors Constructor Description ValidationSupport()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddFailedValidator(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.<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.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.Errorserrors()Provides an instance ofErrorsobject, filled with error messages after validation.Errorserrors(Locale locale)Provides an instance of localizedErrorsobject, filled with error messages after validation.Objectget(String attributeName)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 validation.voidvalidate(boolean reset)Runs validation.NumericValidationBuildervalidateNumericalityOf(String... attributes)ValidationBuildervalidatePresenceOf(String... attributes)ValidationBuildervalidateWith(List<Validator> list)ValidationBuildervalidateWith(Validator validator)List<Validator>validators()
-
-
-
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.
-
validateWith
public ValidationBuilder validateWith(Validator validator)
-
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)
-
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:
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
public void addFailedValidator(Validator validator, String errorKey)
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()
Description copied from interface:ValidatableRuns validation. Will blow away any previous validation errors.- Specified by:
validatein interfaceValidatable
-
validate
public void validate(boolean reset)
Description copied from interface:ValidatableRuns validation.- Specified by:
validatein interfaceValidatable- Parameters:
reset- true to reset all previous validation errors.
-
errors
public 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
public Errors errors(Locale locale)
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.
-
-