Class Errors
java.lang.Object
fc.web.simpleforms.Errors
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addFieldError
(String fieldName, Object msg) Adds a field validation errorvoid
addFieldWarning
(String fieldname, Object msg) Adds an arbitrary warning message generated as part of form processing.void
addFormError
(String msg) Adds a form level error, typically associated with the form itself and/or multiple fields as a group.void
addFormWarning
(String msg) Adds an arbitrary warning message generated as part of form processinggetFieldError
(String fieldName) Returns the field error for the specified fieldname or null if no error was found.getFieldWarning
(String fieldname) Returns the warning for the specified field or null if no warning exists.Returns a list of all form errors or null if no errors are present.Returns the list of all form-level warnings or null if no warnings exist for the form.boolean
hasError()
Returns true if there are any form or field errors.boolean
Returns true if there are any warnings.boolean
hasWarning
(String fieldname) Returns true if there are any warnings for the specified fieldstatic void
void
Convenience method to render a field error.void
Convenience method to render a field error.void
renderFormErrors
(Writer out) Convenience method to render all the form errors (if present).
-
Constructor Details
-
Errors
public Errors()
-
-
Method Details
-
addFormError
Adds a form level error, typically associated with the form itself and/or multiple fields as a group. -
addFieldError
Adds a field validation error- Parameters:
fieldName
- the name of the fieldmsg
- 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
Adds an arbitrary warning message generated as part of form processing -
addFieldWarning
Adds an arbitrary warning message generated as part of form processing. This warning is associated with the specified field. -
getFormErrors
Returns a list of all form errors or null if no errors are present. -
getFieldError
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
Returns the warning for the specified field or null if no warning exists. -
hasError
Returns true if there are any form or field errors. (although warnings are allowed) -
hasWarning
Returns true if there are any warnings. -
hasWarning
Returns true if there are any warnings for the specified field -
renderFormErrors
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
Convenience method to render a field error. Invoking this method is a shorthand for saying (where error is an instance of this class):
The above is the same as: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>"); }
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.error.render(out, "some_field_name");
- Throws:
IOException
-
render
Convenience method to render a field error. Invoking this method is a shorthand for saying (where error is an instance of this class):
The above is the same as (for example):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"); }
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.error.render(out, "some_field_name", "inside", "outside");
- Parameters:
inside
- this string is written before the span tag is closedoutside
- this string is written right after the span tag is closed.- Throws:
IOException
-
main
-