public class FormValidationMessageFormat extends Object
A class for converting a FormValidatorResults
object to text.
If validation of a form fails, usually the corresponding error messages have
to be displayed somehow to the user. This class takes a
FormValidationResults
object as input, iterates over the
contained validation error messages and converts them to text. The result is
a string that can be displayed in a message box for instance.
The class can be configured with a number of template strings for specifying the desired output. There are four template strings that are evaluated:
FormValidatorResults
object, for which error messages are found. Whenever a
new field starts the fieldHeaderTemplate
is issued.fieldErrorTemplate
template is
applied.fieldWarningTemplate
template
is applied. If the fieldWarningTemplate
is not defined, the fieldErrorTemplate
is used instead. The output of warning messages can be
suppressed at all by setting the suppressWarnings
property to
true.fieldFooterTemplate
template is output.
Templates are strings that can contain variables for the name of the current
field and the current error message. When processing the templates the
variables are replaced with their current values resulting in the text to be
displayed. For variables the syntax ${...}
is used, which should
be familiar from Ant. The following table lists the supported variables:
Variable | Description |
---|---|
field | Will be replaced by the name of the current field. This variable is allowed in all templates. |
msgCount | Will be replaced by the number of error messages for the current field. This could for instance be used in the header template to give an overview over the number of errors detected for the current input field. |
msg | Will be replaced by the current error message. This variable is only
supported by the fieldErrorTemplate template. |
msgIndex | Will be replaced by the index of the current error message. The messages
of a field are numbered from 1 up to ${msgCount} . With this
variable the index can be added to the resulting text. |
The default initialization leaves the header and footer template empty and
sets the following error template: ${field}: ${msg}\n
. This
results in output like
Field1: Message1 for Field1 Field2: Message1 for Field2 Field2: Message2 for Field2 Field3: Message1 for Field3By carefully designing the templates, more complex output can be generated as in the following example:
${field} ${msgCount} error(s):\n
- ${msg}
\n
Field1 1 error(s): - Message1 for Field1 Field2 2 error(s): - Message1 for Field2 - Message2 for Field2Templates that are null will be ignored.
Implementation note: This class has a mutable state and thus is not thread-safe. However if it is initialized once and the templates are not changed later, it can be shared between multiple threads.
Modifier and Type | Field and Description |
---|---|
static String |
DEF_ERRORS_TEMPLATE
Constant for the default field errors template.
|
static String |
VAR_FIELD
Constant for the field variable.
|
static String |
VAR_MSG
Constant for the msg variable.
|
static String |
VAR_MSG_COUNT
Constant for the msgCount variable.
|
static String |
VAR_MSG_INDEX
Constant for the msgIndex variable.
|
Constructor and Description |
---|
FormValidationMessageFormat()
Creates a new instance of
FormValidationMessageFormat . |
Modifier and Type | Method and Description |
---|---|
protected String |
fetchDisplayName(Form form,
String field)
Determines the display name of the given field.
|
String |
format(FormValidatorResults res,
Form form)
The main formatting method.
|
String |
formatField(FormValidatorResults res,
Form form,
String field)
Transforms all validation messages found in the passed
FormValidatorResults object for the given field name into a text
according to the values of the current templates. |
String |
getFieldErrorTemplate()
Returns the template for the error messages of an input field.
|
String |
getFieldFooterTemplate()
Returns the template for the footer of a field.
|
String |
getFieldHeaderTemplate()
Returns the template for the header of a field.
|
String |
getFieldWarningTemplate()
Returns the template for the warning messages of an input field.
|
boolean |
isSuppressWarnings()
Returns a flag whether warning messages should be suppressed.
|
protected void |
processMessages(StringBuilder buf,
org.apache.commons.lang.text.StrSubstitutor subst,
FormValidatorResults res,
Form form,
String field,
Map<String,String> variables)
Applies the template for the error messages to all messages available for
the current field.
|
void |
setFieldErrorTemplate(String fieldErrorTemplate)
Sets the template for the error messages of an input field.
|
void |
setFieldFooterTemplate(String fieldFooterTemplate)
Sets the template for the footer of a field.
|
void |
setFieldHeaderTemplate(String fieldHeaderTemplate)
Sets the template for the header of a field.
|
void |
setFieldWarningTemplate(String fieldWarningTemplate)
Sets the template for the warning messages of an input field.
|
void |
setSuppressWarnings(boolean suppressWarnings)
Sets a flag whether warning messages should be suppressed.
|
protected void |
setUpVariablesForField(Map<String,String> vars,
FormValidatorResults res,
Form form,
String field)
Initializes the variables for the specified field of the validation
results object.
|
protected void |
setUpVariablesForMessage(Map<String,String> vars,
FormValidatorResults res,
Form form,
String field,
String msg,
int index)
Initializes the variables for an error message.
|
public static final String DEF_ERRORS_TEMPLATE
public static final String VAR_FIELD
public static final String VAR_MSG
public static final String VAR_MSG_COUNT
public static final String VAR_MSG_INDEX
public FormValidationMessageFormat()
FormValidationMessageFormat
.public String getFieldHeaderTemplate()
public void setFieldHeaderTemplate(String fieldHeaderTemplate)
fieldHeaderTemplate
- the template for the header of a fieldpublic String getFieldFooterTemplate()
public void setFieldFooterTemplate(String fieldFooterTemplate)
fieldFooterTemplate
- the template for the footer of a fieldpublic String getFieldErrorTemplate()
public void setFieldErrorTemplate(String fieldErrorTemplate)
fieldErrorTemplate
- the field error templatepublic String getFieldWarningTemplate()
public void setFieldWarningTemplate(String fieldWarningTemplate)
suppressWarnings
property is not set. If suppressWarnings
is
false and no specific template for warning messages is set, the
error template is used.fieldWarningTemplate
- the field warnings templatepublic boolean isSuppressWarnings()
public void setSuppressWarnings(boolean suppressWarnings)
suppressWarnings
- the suppress warnings flagpublic String format(FormValidatorResults res, Form form)
res
- the object with the validation results (can be null,
then the result of this method is null)form
- the current Form
object; this object is used for
obtaining the display names of the error fields; it can be
null, then the field names are usedpublic String formatField(FormValidatorResults res, Form form, String field)
FormValidatorResults
object for the given field name into a text
according to the values of the current templates. This method can be
called to process the messages of a single field only.res
- the object with the validation results (can be null,
then the result of this method is null)form
- the current Form
object; this object is used for
obtaining the display names of the error fields; it can be
null, then the field names are usedfield
- the name of the field in question (if this field cannot be
found, result is an empty string)protected void setUpVariablesForField(Map<String,String> vars, FormValidatorResults res, Form form, String field)
vars
- the map with the variablesres
- the results objectform
- the form object (may be null)field
- the name of the current fieldprotected void setUpVariablesForMessage(Map<String,String> vars, FormValidatorResults res, Form form, String field, String msg, int index)
setUpVariablesForField()
has already been called).vars
- the map with the variablesres
- the results objectform
- the form object (may be null)field
- the name of the current fieldmsg
- the current validation error messageindex
- the index of this messageprotected void processMessages(StringBuilder buf, org.apache.commons.lang.text.StrSubstitutor subst, FormValidatorResults res, Form form, String field, Map<String,String> variables)
buf
- the target buffersubst
- the substitutorres
- the validation resultsform
- the formfield
- the current fieldvariables
- the map with the variablesprotected String fetchDisplayName(Form form, String field)
Form
object is provided, it is used for resolving the display name. Otherwise
the field name is returned.form
- the formfield
- the field nameCopyright © 2016 The JGUIraffe Team. All rights reserved.