Package com.randomnoun.common
Class ErrorList
java.lang.Object
- All Implemented Interfaces:
Serializable
,Cloneable
,Iterable<ErrorList.ErrorData>
,Collection<ErrorList.ErrorData>
,List<ErrorList.ErrorData>
,RandomAccess
,SequencedCollection<ErrorList.ErrorData>
The ErrorList class is a a fairly generic class for containing validation errors for
input forms, similar to the struts ActionErrors class. Each error within this class can
contain a 'short' and 'long' description, where the short description is normally a
categorisation of the error and the longer description describes what has happened and
how to fix the problem; (e.g. shortText="Missing field",
longText="The field 'id' is mandatory. Please enter a value for this field."). The short
description is normally rendered by
errorHeader.jsp
in bold before the long
description. An individual error also may contain a list of fields that it applies to
(e.g. a 'mandatory inclusive' error may apply to several fields at once), and a
severity (normally set to SEVERITY_INVALID
. Errors are displayed by the
errorHeader.jsp
JSP, which is typically included at the top of any page
that contains an input form. An error without a severity supplied is assumed to be
of SEVERITY_INVALID, and an error without any fields supplied is assumed to be
associated with the entire form, rather than a specific set of fields.
Errors are inserted into an ErrorList by using one of the addError methods:
-
addError(String, String)
- add an invalid form error -
addError(String, String, int)
- add an form error with a specific severity -
addError(String, String, String)
- add an invalid field error -
addError(String, String, String, int)
- add an invalid field error with a specific severity
Code that uses an ErrorList to perform validation may attach an object to the errorList instance to perform standard validations and to localise error messages.
- Author:
- knoxg
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
ErrorInfo inner class - contains information related to a single error -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
invalid system-supplied data; end-user cannot fix situationstatic final int
internal error (e.g.static final int
information message; can be used in addition to SEVERITY_OK for additional textstatic final int
invalid user-supplied data; end-user can fix situationstatic final int
Severity level indicating 'not an error' (e.g.static final int
unrecoverable internal error (can't think of anything here, but the world ending could be onestatic final int
possibly incorrect user-supplied data; operation still succeeds but may return incorrect resultsFields inherited from class java.util.AbstractList
modCount
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds an error that isn't associated with any particular field, with a default severity ofSEVERITY_ERROR
.void
Adds an error that isn't associated with any particular fieldvoid
As peraddError(String, String, String, int)
, with a default severity ofSEVERITY_ERROR
.void
Adds an error.boolean
Appends the errors contained within another ErrorData into this one.void
Resets all errors contained within this objectReturns the bundle used for localising validation messagesgetFieldAt
(int pos) Returns the field of the pos'th errorgetFieldName
(String field) getFieldValue
(String name) Returns the value of a field from the attached objectThe locale in which validation messages will be localisedgetLocalisedFieldName
(String field) Returns a localised form of the supplied field name, to be used within generic validation messagesgetLongTextAt
(int pos) Returns the longText of the pos'th errorThe object containing the data being validatedint
getSeverityAt
(int pos) Returns the severity of the pos'th errorgetShortTextAt
(int pos) Returns the shortText string of the pos'th errorint
getSize()
Same assize()
.boolean
hasErrorOn
(String field) Returns true if an error has occured on the CGI field passed as a parameter to this methodboolean
Returns true if there are any errors in this objectboolean
hasErrors
(int severity) Returns true if there are any errors of the specified severity or higherint
Returns the maximum severity of all current errors.void
void
setValidatedObject
(Object attachedObject, String attachedFieldFormat, ResourceBundle attachedBundle, String bundleFormat, Locale locale) Attaches an object to be validated to this ErrorList.int
size()
Returns the number of errors in this objecttoJSON()
Return the JSON representation of this objecttoString()
Return all the errors within this object in a single string.void
unattach()
Removes validation metadata from this object (i.e.Methods inherited from class java.util.ArrayList
add, add, addAll, addAll, addFirst, addLast, clear, clone, contains, ensureCapacity, equals, forEach, get, getFirst, getLast, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeFirst, removeIf, removeLast, removeRange, replaceAll, retainAll, set, sort, spliterator, subList, toArray, toArray, trimToSize
Methods inherited from class java.util.AbstractCollection
containsAll
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, stream, toArray
Methods inherited from interface java.util.List
containsAll, reversed
-
Field Details
-
SEVERITY_OK
Severity level indicating 'not an error' (e.g. informational only). Used to indicate successful operations- See Also:
-
SEVERITY_INVALID
invalid user-supplied data; end-user can fix situation- See Also:
-
SEVERITY_ERROR
invalid system-supplied data; end-user cannot fix situation- See Also:
-
SEVERITY_FATAL
internal error (e.g. EJB/LDAP connection failure)- See Also:
-
SEVERITY_PANIC
unrecoverable internal error (can't think of anything here, but the world ending could be one- See Also:
-
SEVERITY_INFO
information message; can be used in addition to SEVERITY_OK for additional text- See Also:
-
SEVERITY_WARNING
possibly incorrect user-supplied data; operation still succeeds but may return incorrect results- See Also:
-
-
Constructor Details
-
ErrorList
public ErrorList()Create a new, empty ErrorData object.
-
-
Method Details
-
addError
Adds an error. This is the "real" addError() method; all others delegate to this one.- Parameters:
errorField
- a comma-separated list of field names that caused this errorshortText
- a short string conveying the error type (e.g. 'Missing field')longText
- a longer string describing the nature of the error and how to resolve it (e.g. 'The field 'id' is mandatory. Please enter a value for this field.')severity
- the severity of this error (one of the SEVERITY_* constants of this class).- See Also:
-
addError
As peraddError(String, String, String, int)
, with a default severity ofSEVERITY_ERROR
.- Parameters:
errorField
- a comma-separated list of field names that caused this errorshortText
- a short string conveying the error type (e.g. 'Missing field')longText
- a longer string describing the nature of the error and how to resolve it (e.g. 'The field 'id' is mandatory. Please enter a value for this field.')
-
addError
Adds an error that isn't associated with any particular field- Parameters:
shortText
- a short string conveying the error type (e.g. 'Missing field')longText
- a longer string describing the nature of the error and how to resolve it (e.g. 'The field 'id' is mandatory. Please enter a value for this field.')severity
- the severity of this error (one of the SEVERITY_* constants of this class)
-
addError
Adds an error that isn't associated with any particular field, with a default severity ofSEVERITY_ERROR
.- Parameters:
shortText
- a short string conveying the error type (e.g. 'Missing field')longText
- a longer string describing the nature of the error and how to resolve it (e.g. 'The field 'id' is mandatory. Please enter a value for this field.')
-
clearErrors
Resets all errors contained within this object -
addErrors
Appends the errors contained within another ErrorData into this one.- Parameters:
errorList
-- Returns:
- true if there were any additional errors to embed and at least one of the errors had a severity of SEVERITY_ERROR or higher (worse), false otherwise.
-
hasErrorOn
Returns true if an error has occured on the CGI field passed as a parameter to this method- Returns:
- true if an error occured, false if not.
-
hasErrors
Returns true if there are any errors in this object- Returns:
- True if the number of errors > 0
-
hasErrors
Returns true if there are any errors of the specified severity or higher- Parameters:
severity
- a SEVERITY_* constant- Returns:
- True if the number of errors at the specified severity or higher > 0
-
maxErrorSeverity
Returns the maximum severity of all current errors.- Returns:
- the severity ranking of the most severe error, or -1 if there are no errors contained within this ErrorData object.
-
size
Returns the number of errors in this object- Specified by:
size
in interfaceCollection<ErrorList.ErrorData>
- Specified by:
size
in interfaceList<ErrorList.ErrorData>
- Overrides:
size
in classArrayList<ErrorList.ErrorData>
- Returns:
- The number of errors in this object
-
getSize
Same assize()
. Only here to allow us to access the object in JSTL- Returns:
- The number of errors in this object
-
getShortTextAt
Returns the shortText string of the pos'th error -
getLongTextAt
Returns the longText of the pos'th error -
getFieldAt
Returns the field of the pos'th error -
getSeverityAt
Returns the severity of the pos'th error -
toString
Return all the errors within this object in a single string. Suitable for inclusion within email alarms, logs etc...- Overrides:
toString
in classAbstractCollection<ErrorList.ErrorData>
- Returns:
- Newline-separated list of errors
-
setValidatedObject
public void setValidatedObject(Object attachedObject, String attachedFieldFormat, ResourceBundle attachedBundle, String bundleFormat, Locale locale) Attaches an object to be validated to this ErrorList.- Parameters:
attachedObject
- a POJO, a HttpServletRequest, or a Map (e.g. request.getParameterMap()). It identifies where the validated data is coming from.attachedBundle
- a bundle where the field names for this object are to be retrieved from.bundleFormat
- a MessageFormat used to to retrieve field names from attachedBundle. The "{0}" placeholder in this String is replaced with the name of the field being validated.
-
resetValidatedObject
-
getObject
The object containing the data being validated- Returns:
- the object containing the data being validated
-
getLocale
The locale in which validation messages will be localised- Returns:
- The locale in which validation messages will be localised
-
getFieldValue
Returns the value of a field from the attached object- Parameters:
name
- field name- Returns:
- field value
-
getBundle
Returns the bundle used for localising validation messages- Returns:
- the bundle used for localising validation messages
-
getLocalisedFieldName
Returns a localised form of the supplied field name, to be used within generic validation messages- Parameters:
field
- field name- Returns:
- localised form of field name
-
getFieldName
-
unattach
Removes validation metadata from this object (i.e. attached objects, bundles, bundleFormats) -
toJSON
Return the JSON representation of this object- Returns:
- the JSON representation of this object
-