public abstract class Field extends Object
Note: A FieldValidator
for a given field is added to that field
automatically by a new Validator when that validator is instantiated (so a
public addValidator method is not needed in this class).
Fields are normally created and added to a form once. However, new fields can
also be created per user/per-submit and added to the FormData
object.
These fields can then be rendered via the Form.renderDynamicFields(fc.web.forms.FormData, java.io.Writer)
method and are shown just for that request. They are not part of the form and
cannot be later retrieved by Form.get(java.lang.String)
-- use {Form#getSubmittedField}
instead.
Initial values (if any) for fields are typically specified during form
instantiation. These default values are initially shown to all users when
the form is rendered. However, in some cases, it is possible to show
different initial values to different users. This is done by creating a new
FormData
object before the form is rendered and calling the
setValue(FormData, ...) methods on various fields to set values
on the formdata object.
FormData objects are automatically created to store submitted form data
(and maintain form state) by the Form.handleSubmit(javax.servlet.http.HttpServletRequest)
method. By manually
creating a new FormData object before showing the form, and the using that
object to render the form, per-user initial values can be displayed (this
simulates a user form submission which of course is different for each
user).
This form api is complex. For a simpler bare-bones alternative, see
fc.web.simpleforms
Modifier and Type | Class and Description |
---|---|
static class |
Field.Type
Encapsulates different types of HTML form elements.
|
Constructor and Description |
---|
Field(String fieldName)
Creates a new form Field.
|
Modifier and Type | Method and Description |
---|---|
void |
add(FieldRefresher refresher)
Adds a refresher to this field.
|
void |
addError(FormData fd,
String errorMessage)
Adds a field level validation error message.
|
void |
addLabel(String label)
Adds some arbitrary text to this field which can later be retrieved via the
getLabel method and shown as an html label for this field.
|
void |
addString(String str)
Adds any arbitrary string to the field, which is written as is when
the field is rendered.
|
void |
disable()
Disables this field for all users.
|
void |
disable(FormData fd)
Disabled this field for this particular request.
|
void |
enable()
Enables this field for all users.
|
void |
enable(FormData fd)
Enables this field for this particular request.
|
String |
getLabel()
Returns the previously set label or null if no
label was set.
|
String |
getName()
Returns the name of this form element
|
abstract Field.Type |
getType()
Subclasses should return an appropriate
Field.Type . |
List |
getValidateErrors(FormData fd)
Returns a List of Strings containing validation errors.
|
boolean |
isEnabled(FormData fd) |
abstract boolean |
isFilled(FormData fd)
Returns true if this field was isFilled out or selected by the
user, false otherwise.
|
void |
render(FormData fd,
Writer writer)
This method writes the HTML for this form field to the given writer.
|
void |
render(FormData fd,
Writer writer,
String prefix,
String suffix)
Similar to
render(Writer) but also renders the specified
prefix/suffix strings before and after each element. |
void |
render(Writer writer)
Convenience method that calls render(null, writer)
(for when the form needs to be displayed the first time
without any formdata)
|
Field |
renderError(FormData fd,
Writer writer)
Calls
renderError(Writer, String) with <br> as
the message seperator string. |
Field |
renderError(FormData fd,
Writer writer,
String htmlSep)
Convenience method to render validation errors.
|
abstract void |
renderImpl(FormData fd,
Writer writer) |
void |
renderStyleTag(boolean val)
Set to true if the element should render a style tag, false
otherwise.
|
void |
setStyleTag(String value)
Sets a style tag to be rendered (overrides the default class tag of
formName-inputName.
|
abstract void |
setValueFromSubmit(FormData fd,
javax.servlet.http.HttpServletRequest req)
This method sets the value of this field from the parameters obtained from
the specified request.
|
String |
toString() |
boolean |
validate(FormData fd,
javax.servlet.http.HttpServletRequest req)
Validates this field via the installed validators.
|
public abstract Field.Type getType()
Field.Type
. This type
is rendered as part of <input type= ...public abstract void setValueFromSubmit(FormData fd, javax.servlet.http.HttpServletRequest req) throws SubmitHackedException
fd
- the form data object to store the value inSubmitHackedException
public abstract boolean isFilled(FormData fd)
Note: Some fields like selects will never be empty since non-multiple select fields always send their default selected value. [although select/with/multiple can be empty since the browser sends (much like radio buttoms) nothing at all when no option is selected].
public abstract void renderImpl(FormData fd, Writer writer) throws SQLException, IOException
SQLException
IOException
public void render(FormData fd, Writer writer) throws SQLException, IOException
Important Note: Although each field renders itself, validation errors are not rendered as part of this rendering. This is specifically designed for flexibility in HTML layout and display of error messages. This does imply that page authors must remember to extract and write error messages from each field.
Important Note 2: If providing a non-null argument for the FormData
parameter, this method should only be called after calling Form.handleSubmit(javax.servlet.http.HttpServletRequest)
. FormData objects hold the results of submitted form
data. If the formdata is null, it implies that the form is
shown to the user for the first time. The fields then render themselves
based on their default/initial values.. However, one can also display
different initial values unique to a user (say by retrieving their last
filled values from a database). In that case, a FormData object should be
initially/manually created and passed to this method.
fd
- the current form data object (can be null).writer
- the output destinationSQLException
IOException
public void render(Writer writer) throws SQLException, IOException
SQLException
IOException
public void render(FormData fd, Writer writer, String prefix, String suffix) throws SQLException, IOException
render(Writer)
but also renders the specified
prefix/suffix strings before and after each element. For example, one can
say (in a jsp):
<% form.get("foo").render(); %>
or:
<% form.get("foo").render("", "
"); %>
fd
- the current form data object (if any). Form data objects
hold the results of submitted form data. This might be
null initially (when the form is show to the user
for the first time).writer
- the output destinationprefix
- prefix value for each element. specify
null or an empty string if not needed.suffix
- suffix value for each element. specify
null or an empty string if not needed.SQLException
IOException
public Field renderError(FormData fd, Writer writer) throws IOException
renderError(Writer, String)
with <br> as
the message seperator string.IOException
public Field renderError(FormData fd, Writer writer, String htmlSep) throws IOException
getValidateErrors(fc.web.forms.FormData)
and:
This method does exactly that: if applicable it writes validation error messages, each seperated by the specfied message seperator parameter.
If this field is rendering style tags, then the error messages are encapulated in a html <span> tag and the class of the span tag is set to form-errmsg
writer
- the Writer to render errors tohtmlSep
- the seperator string (any string or html tag/fragment)
to separate messages (if there are more than 1
error messages)IOException
public void addError(FormData fd, String errorMessage)
FieldValidator
(these classes are used outside of the
validation framework and their error message still needs to be added to the
field validation error data)
This is a very useful method for adding the results of arbitrary code to this form.
public void add(FieldRefresher refresher)
public void addString(String str)
setStyleTag(java.lang.String)
however).
This method can be called as many times as needed. For example:
or to set a arbitrary Javascript string:addString("tabindex=2"); addString("onclick='foo();return true;'");
addString("onMouseOver='bar()' onClick='foo()'");
public void addLabel(String label)
public boolean validate(FormData fd, javax.servlet.http.HttpServletRequest req)
setValueFromSubmit(fc.web.forms.FormData, javax.servlet.http.HttpServletRequest)
method.
No validators are run if the field is not enabled.
Validation errors that result after calling this method (if any)
can be retrieved via the getValidateErrors(fc.web.forms.FormData)
method.
public List getValidateErrors(FormData fd)
public void enable(FormData fd)
Note, since we use the FormData
to keep track of enable/disable
status, fields can be enabled/disabled per user (independent of other
users).
public void disable(FormData fd)
Note, since we use the FormData
to keep track of enable/disable
status, fields can be enabled/disabled per user (indepenent of other
users).
public void enable()
public void disable()
public void setStyleTag(String value)
public void renderStyleTag(boolean val)
An arbitrary style class can be set for a field by calling the setStyleTag(java.lang.String)
method. If a custom style class is not set via this
method, style tags are rendered as:
So, for example, a input type of name myname contained in the form myform will render it's style tag as the following class:class=formName-inputName
Note, . and _ are not allowed in css class names (for legacy reasons) but - is allowed.