public final class Errors extends Object
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 and Description |
---|
Errors() |
Modifier and Type | Method and Description |
---|---|
void |
addFieldError(String fieldName,
Object msg)
Adds a field validation error
|
void |
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 processing
|
Object |
getFieldError(String fieldName)
Returns the field error for the specified fieldname or null if no
error was found.
|
Object |
getFieldWarning(String fieldname)
Returns the warning for the specified field or null if
no warning exists.
|
List |
getFormErrors()
Returns a list of all form errors or null if no errors are present.
|
List |
getFormWarnings()
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 |
hasWarning()
Returns true if there are any warnings.
|
boolean |
hasWarning(String fieldname)
Returns true if there are any warnings for the specified field
|
static void |
main(String[] args) |
void |
render(Writer out,
String fieldName)
Convenience method to render a field error.
|
void |
render(Writer out,
String fieldName,
String inside,
String outside)
Convenience method to render a field error.
|
void |
renderFormErrors(Writer out)
Convenience method to render all the form errors (if present).
|
public Errors()
public void addFormError(String msg)
public void addFieldError(String fieldName, Object msg)
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 fieldpublic void addFormWarning(String msg)
public void addFieldWarning(String fieldname, Object msg)
public List getFormErrors()
public Object getFieldError(String fieldName)
public List getFormWarnings()
public Object getFieldWarning(String fieldname)
public boolean hasError()
public boolean hasWarning()
public boolean hasWarning(String fieldname)
public void renderFormErrors(Writer out) throws IOException
String after = "<br>"; List list = error.getFormErrors(); if (list != null) { out.write("<div class='form-errmsg'>"); out.write("<ul>"); for (int n = 0; n < list.size(); n++) { out.write("<li>"); out.write(String.valueOf(list.get(n))); out.write("</li>"); } out.write("</ul>"); out.write("</div>\n"); }
IOException
public void render(Writer out, String fieldName) throws IOException
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");
IOException
public void render(Writer out, String fieldName, String inside, String outside) throws IOException
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");
inside
- this string is written before the span tag is closedoutside
- this string is written right after the span tag is closed.IOException