Class Errors

java.lang.Object
fc.web.simpleforms.Errors

public final class Errors extends Object
Convenience class to store arbitrary form validation errors and messages. This class should be instantiated per request as needed (when there are form validation errors).

Note: This class is not thread-safe but that's not a concern since only each seperate user request is handled by at most 1 thread.

  • Constructor Details

  • Method Details

    • addFormError

      public void addFormError(String msg)
      Adds a form level error, typically associated with the form itself and/or multiple fields as a group.
    • addFieldError

      public void addFieldError(String fieldName, Object msg)
      Adds a field validation error
      Parameters:
      fieldName - the name of the field
      msg - some error object, typically a string but can be a list of strings (for example) if there is more than 1 validation error for this field
    • addFormWarning

      public void addFormWarning(String msg)
      Adds an arbitrary warning message generated as part of form processing
    • addFieldWarning

      public void addFieldWarning(String fieldname, Object msg)
      Adds an arbitrary warning message generated as part of form processing. This warning is associated with the specified field.
    • getFormErrors

      public List getFormErrors()
      Returns a list of all form errors or null if no errors are present.
    • getFieldError

      public Object getFieldError(String fieldName)
      Returns the field error for the specified fieldname or null if no error was found.
    • getFormWarnings

      Returns the list of all form-level warnings or null if no warnings exist for the form.
    • getFieldWarning

      public Object getFieldWarning(String fieldname)
      Returns the warning for the specified field or null if no warning exists.
    • hasError

      public boolean hasError()
      Returns true if there are any form or field errors. (although warnings are allowed)
    • hasWarning

      public boolean hasWarning()
      Returns true if there are any warnings.
    • hasWarning

      public boolean hasWarning(String fieldname)
      Returns true if there are any warnings for the specified field
    • renderFormErrors

      public void renderFormErrors(Writer out) throws IOException
      Convenience method to render all the form errors (if present). For more control, obtain the form errors and print them manually. Invoking this method has the following effect:
      String after = "<br>";
      List list = error.getFormErrors();
      if (list != null) 
        {
          out.write("<div class='form-errmsg'>");
          out.write("<ul>");
          for (int n = 0; n invalid input: '<' list.size(); n++) {
            out.write("<li>");
              out.write(String.valueOf(list.get(n)));
              out.write("</li>");
              }
          out.write("</ul>");
          out.write("</div>\n");
          }
      
      Throws:
      IOException
    • render

      public void render(Writer out, String fieldName) throws IOException
      Convenience method to render a field error. Invoking this method is a shorthand for saying (where error is an instance of this class):
      Object obj = error.getFieldError("some_field_name");
      if (str != null) {
          out.write("<span class='field-errmsg'>");
          out.write (String.valueOf(obj));
          out.write("</span>\n");
          out.write("<br>"); 
          }
      
      The above is the same as:
      error.render(out, "some_field_name");
      
      Note: The object representing the error for the field is written as is. Typically for strings, this works fine. However, for more complex objects (like say a list holding more than 1 error for the same field), the list is printed as-is. For more formatting options for complex objects, obtain and print the error manually.
      Throws:
      IOException
    • render

      public void render(Writer out, String fieldName, String inside, String outside) throws IOException
      Convenience method to render a field error. Invoking this method is a shorthand for saying (where error is an instance of this class):
      Object obj = error.getFieldError("some_field_name");
      if (str != null) {
          out.write("<span class='field-errmsg'>");
          out.write (String.valueOf(obj));
          out.write("inside");
          out.write("</span>\n");
          out.write("outside"); 
          }
      
      The above is the same as (for example):
      error.render(out, "some_field_name", "inside", "outside");
      
      Note: The object representing the error for the field is written as is. Typically for strings, this works fine. However, for more complex objects (like say a list holding more than 1 error for the same field), the list is printed as-is. For more formatting options for complex objects, obtain and print the error manually.
      Parameters:
      inside - this string is written before the span tag is closed
      outside - this string is written right after the span tag is closed.
      Throws:
      IOException
    • main

      public static void main(String[] args) throws Exception
      Throws:
      Exception