Package org.apache.wicket.validation
Class ValidationError
- java.lang.Object
-
- org.apache.wicket.validation.ValidationError
-
- All Implemented Interfaces:
java.io.Serializable,org.apache.wicket.util.io.IClusterable,IValidationError
public final class ValidationError extends java.lang.Object implements IValidationError
A versatile implementation ofIValidationErrorthat supports message resolution fromIErrorMessageSource, default message (if none of the keys matched), and variable substitution. The final error message is constructed via the following process:- Try all keys added by calls to
addKey(String)via the providedIErrorMessageSource. - If none of the keys yielded a message, use the message set by
setMessage(String), if any. - Perform variable substitution on the message, if any.
- Since:
- 1.2.6
- Author:
- Igor Vaynberg (ivaynberg)
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description ValidationError()Constructs an empty errorValidationError(java.lang.String message)Constructs a validation error with the specified message.ValidationError(IValidator<?> validator)Constructs a validation error with the validator's standard key.ValidationError(IValidator<?> validator, java.lang.String variation)Constructs a validation error with a variation of validator's standard key.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ValidationErroraddKey(java.lang.String key)Adds a key to the list of keys that will be tried againstIErrorMessageSourceto locate the error message string.ValidationErroraddKey(IValidator<?> validator)Shortcut for adding a standard message key which is the simple name of the validator' classValidationErroraddKey(IValidator<?> validator, java.lang.String variation)Shortcut for adding a standard message key variation which is the simple name of the validator class followed by a dot and the variationjava.io.SerializablegetErrorMessage(IErrorMessageSource messageSource)Retrieves the error message (usually user-facing).java.util.List<java.lang.String>getKeys()Gets error keysjava.lang.StringgetMessage()Gets the default message that will be used when no message could be located via message keys.java.util.Map<java.lang.String,java.lang.Object>getVariables()Retrieves the variables map for this error.voidsetKeys(java.util.List<java.lang.String> keys)Sets error keysValidationErrorsetMessage(java.lang.String message)Sets message that will be used when no message could be located via message keys.ValidationErrorsetVariable(java.lang.String name, java.lang.Object value)Sets a key and value in the variables map for use in substitution.ValidationErrorsetVariables(java.util.Map<java.lang.String,java.lang.Object> vars)Sets the variables map for this error.java.lang.StringtoString()
-
-
-
Constructor Detail
-
ValidationError
public ValidationError()
Constructs an empty error
-
ValidationError
public ValidationError(IValidator<?> validator)
Constructs a validation error with the validator's standard key. Equivalent to callingaddKey(IValidator)- Parameters:
validator- validator
-
ValidationError
public ValidationError(IValidator<?> validator, java.lang.String variation)
Constructs a validation error with a variation of validator's standard key. Equivalent to callingaddKey(IValidator, String)- Parameters:
validator- validatorvariation- key variation
-
ValidationError
public ValidationError(java.lang.String message)
Constructs a validation error with the specified message. Equivalent to callingsetMessage(String)- Parameters:
message- message
-
-
Method Detail
-
addKey
public ValidationError addKey(java.lang.String key)
Adds a key to the list of keys that will be tried againstIErrorMessageSourceto locate the error message string.- Parameters:
key- a message key to be added- Returns:
- this
ValidationErrorfor chaining purposes
-
addKey
public ValidationError addKey(IValidator<?> validator)
Shortcut for adding a standard message key which is the simple name of the validator' class- Parameters:
validator- validator- Returns:
this
-
addKey
public ValidationError addKey(IValidator<?> validator, java.lang.String variation)
Shortcut for adding a standard message key variation which is the simple name of the validator class followed by a dot and the variationIf the variation is empty only the validator's simple class name is used
- Parameters:
validator- validatorvariation- key variation- Returns:
this
-
setVariable
public ValidationError setVariable(java.lang.String name, java.lang.Object value)
Sets a key and value in the variables map for use in substitution.- Parameters:
name- a variable namevalue- a variable value- Returns:
- this
ValidationErrorfor chaining purposes
-
getVariables
public final java.util.Map<java.lang.String,java.lang.Object> getVariables()
Retrieves the variables map for this error. The caller is free to modify the contents.- Returns:
- a
Mapof variables for this error
-
setVariables
public final ValidationError setVariables(java.util.Map<java.lang.String,java.lang.Object> vars)
Sets the variables map for this error.- Parameters:
vars- a variables map- Returns:
- this
ValidationErrorfor chaining purposes
-
getErrorMessage
public final java.io.Serializable getErrorMessage(IErrorMessageSource messageSource)
Description copied from interface:IValidationErrorRetrieves the error message (usually user-facing).- Specified by:
getErrorMessagein interfaceIValidationError- Parameters:
messageSource- the message source- Returns:
- the error message
- See Also:
IValidationError.getErrorMessage(IErrorMessageSource)
-
getMessage
public final java.lang.String getMessage()
Gets the default message that will be used when no message could be located via message keys.- Returns:
- message the default message used when all keys yield no message
-
setMessage
public final ValidationError setMessage(java.lang.String message)
Sets message that will be used when no message could be located via message keys.Note: No variable substitution is performed on the given message!
- Parameters:
message- a default message to be used when all keys yield no message- Returns:
- this
ValidationErrorfor chaining purposes
-
getKeys
public java.util.List<java.lang.String> getKeys()
Gets error keys- Returns:
- keys
-
setKeys
public void setKeys(java.util.List<java.lang.String> keys)
Sets error keys- Parameters:
keys-
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object- See Also:
Object.toString()
-
-