Class ErrorList

All Implemented Interfaces:
Serializable, Cloneable, Iterable<ErrorList.ErrorData>, Collection<ErrorList.ErrorData>, List<ErrorList.ErrorData>, RandomAccess, SequencedCollection<ErrorList.ErrorData>

public class ErrorList extends ArrayList<ErrorList.ErrorData> implements Serializable
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:

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:
  • Field Details

  • Constructor Details

    • ErrorList

      public ErrorList()
      Create a new, empty ErrorData object.
  • Method Details

    • addError

      public void addError(String errorField, String shortText, String longText, int severity)
      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 error
      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).
      See Also:
    • addError

      public void addError(String errorField, String shortText, String longText)
      As per addError(String, String, String, int), with a default severity of SEVERITY_ERROR.
      Parameters:
      errorField - a comma-separated list of field names that caused this error
      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.')
    • addError

      public void addError(String shortText, String longText, int severity)
      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

      public void addError(String shortText, String longText)
      Adds an error that isn't associated with any particular field, with a default severity of SEVERITY_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

      public void clearErrors()
      Resets all errors contained within this object
    • addErrors

      public boolean addErrors(ErrorList errorList)
      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

      public boolean hasErrorOn(String field)
      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

      public boolean hasErrors()
      Returns true if there are any errors in this object
      Returns:
      True if the number of errors > 0
    • hasErrors

      public boolean hasErrors(int severity)
      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

      public int 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

      public int size()
      Returns the number of errors in this object
      Specified by:
      size in interface Collection<ErrorList.ErrorData>
      Specified by:
      size in interface List<ErrorList.ErrorData>
      Overrides:
      size in class ArrayList<ErrorList.ErrorData>
      Returns:
      The number of errors in this object
    • getSize

      public int getSize()
      Same as size(). Only here to allow us to access the object in JSTL
      Returns:
      The number of errors in this object
    • getShortTextAt

      public String getShortTextAt(int pos)
      Returns the shortText string of the pos'th error
    • getLongTextAt

      public String getLongTextAt(int pos)
      Returns the longText of the pos'th error
    • getFieldAt

      public String getFieldAt(int pos)
      Returns the field of the pos'th error
    • getSeverityAt

      public int getSeverityAt(int pos)
      Returns the severity of the pos'th error
    • toString

      public String toString()
      Return all the errors within this object in a single string. Suitable for inclusion within email alarms, logs etc...
      Overrides:
      toString in class AbstractCollection<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

      public void resetValidatedObject()
    • getObject

      public Object getObject()
      The object containing the data being validated
      Returns:
      the object containing the data being validated
    • getLocale

      public Locale getLocale()
      The locale in which validation messages will be localised
      Returns:
      The locale in which validation messages will be localised
    • getFieldValue

      public String getFieldValue(String name)
      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

      public String getFieldName(String field)
    • unattach

      public void unattach()
      Removes validation metadata from this object (i.e. attached objects, bundles, bundleFormats)
    • toJSON

      public String toJSON()
      Return the JSON representation of this object
      Returns:
      the JSON representation of this object