public class Form extends Object
Method in this class are thread safe since form processing is done
entirely within various methods and the form processing results in the
creation of seperate FormData
objects for each request.
Various Fields
can be added to the form at form creation
time. Some fields (like Hidden
can also be added per user/request,
dynamically, to the FormData
object. This is useful while saving
some user or request specific information.
A form collects data from the front-end/web-client and often saves it to a back end database entity. Form data validation can be done in 2 ways:
An HTML form presented to the user can contain fields from more than 1 database table. For example, there could to 2 database tables, say "person" and "office" both with a "other" column. Since each field contained in the form object must have a unique name, there will be a name clash between the fields/attributes of the entities when 2 fields with the name "other" are added to the form.
There are 2 general approaches to solving this problem.
Sometimes, other methods need to obtain a field (for a particular table) by
name. For example, the managers generated by Generate
have a addValidators(form) method that extract the fields for a
particular table and add validators to that field. This method in
particular need to be passed either the appropriate form object in which
that field exists (if there are multiple form objects) or in the case of
only 1 form object, passed the prefix for that particular field, if that
field was added with a prefix to it's name.
Modifier and Type | Field and Description |
---|---|
static List |
empty_list |
static Map |
empty_map |
Constructor and Description |
---|
Form(String name)
Creates a new form with the specified name, method=POST and no action URL
|
Form(String name,
String method,
String actionURL)
Creates a new form with logging to the default log retreived by
Log.getDefault() and the default SubmitHackedHandler |
Form(String name,
String method,
String actionURL,
Log log) |
Form(String name,
String method,
String actionURL,
Log log,
SubmitHackedHandler handler)
Creates a new form with the specified name, method (get/post) and URL
|
Modifier and Type | Method and Description |
---|---|
Form |
add(Dependency d)
Adds the specified dependency to the form.
|
Form |
add(Field field)
Adds the specified element to the form.
|
Form |
add(FieldRef ref)
Adds a
FieldRef to this Form. |
void |
addDynamicField(FormData fd,
Hidden f)
Adds a
Hidden field to this form. |
void |
addError(FormData fd,
String errorMessage)
Convenience method that invokes
addError with an empty validation errorKey. |
void |
addError(FormData fd,
String errorKey,
String errorMessage)
Adds a form level validation error message.
|
void |
addMessage(FormData fd,
String key,
String value)
Adds an arbitrary message for this request.
|
void |
addSubmitHackedHandler(SubmitHackedHandler handler) |
void |
addValidator(FormValidator validator)
Adds a form level validator to this form.
|
boolean |
containsField(String fieldName)
Returns true if the form contains the specified field.
|
void |
destroy()
This method should be called when the form is removed from session or
application scope.
|
Field |
get(String name)
Returns the form field with the specified name.
|
String |
getActionURL()
Returns the action URL string.
|
Collection |
getAllFields()
Returns a collection view of all the fields contained in this
form.
|
Checkbox |
getCheckbox(String name)
Returns the field with the specific name as a
Checkbox . |
CheckboxGroup |
getCheckboxGroup(String name)
Returns the field with the specific name as a
Checkbox . |
List |
getContaining(String name)
Returns all the fields that contains the specified name.
|
String |
getDisplayURL()
Returns the display URL string.
|
Hidden |
getDynamicField(FormData fd,
String fieldName)
Gets a
Hidden field added to the form data object. |
Map |
getFieldErrors(FormData fd) |
FieldRef |
getFieldRef(String key)
Returns the
FieldRef with the specified key. |
Collection |
getFormErrorsCollection(FormData fd) |
List |
getFormErrorsFor(FormData fd,
String formValidatorName)
Returns a list containing all form-level errors for the specified
validator.
|
Hidden |
getHidden(String name)
Returns the field with the specific name as a
Hidden . |
String |
getMessage(FormData fd,
String key)
Retrieves a previously added message or null if the speccified
form data was null or if the message was not found.
|
String |
getName()
Returns the name of this form
|
Password |
getPassword(String name)
Returns the field with the specific name as a
Password . |
Radio |
getRadio(String name)
Returns the field with the specific name as a
Radio . |
RadioGroup |
getRadioGroup(String name)
Returns the field with the specific name as a
Checkbox . |
Select |
getSelect(String name)
Returns the field with the specific name as a
Select . |
Hidden |
getSubmittedField(javax.servlet.http.HttpServletRequest req,
String name)
Once the form is displayed to the user (with, say, hidden fields that
were added dynamically for that request), then when that form is
submitted, those dynamic fields can be retrieved via any of:
the
ServletRequest.getParameter(java.lang.String) method (often the easiest
way)
the #getSubmittedField(HttpServletRequest) method (which will
attempt to read the request parameter with the specified name and return
is as a Hidden field. |
Text |
getText(String name)
Returns the field with the specific name as a
Text . |
TextArea |
getTextArea(String name)
Returns the field with the specific name as a
TextArea . |
Timer |
getTimer() |
RefreshableSelect |
getUpdatableSelect(String name)
Returns the field with the specific name as a
RefreshableSelect . |
FormData |
handleSubmit(javax.servlet.http.HttpServletRequest req)
Processes the submitted form.
|
FormData |
handleSubmit(javax.servlet.http.HttpServletRequest req,
Connection con)
This method is similar to
handleSubmit(HttpServletRequest) except
it stores the specified connection in the form data. |
static boolean |
hasError(FormData fd)
Returns true if validation did not succeed.
|
FormData |
refresh(javax.servlet.http.HttpServletRequest req) |
FormData |
refresh(javax.servlet.http.HttpServletRequest req,
Connection con)
Refreshes the form for this request by running all attached refreshers
and dependencies.
|
Object |
remove(Field field)
Removes the specified field from the form.
|
void |
renderDynamicFields(FormData fd,
Writer out)
Renders all dynamically added hidden fields of the form.
|
void |
renderError(FormData fd,
Writer writer)
Convenience method to render form-level validation errors.
|
void |
renderErrorFor(FormData fd,
Writer writer,
String formValidatorName)
Convenience method to render a validation error for a particluar form level
validator.
|
void |
setActionURL(String url)
Sets an arbitrary application specific string that can
later be retrieved.
|
void |
setDisplayURL(String url)
Sets an arbitrary application specific string.
|
String |
toString() |
public static final List empty_list
public Form(String name, String method, String actionURL, Log log, SubmitHackedHandler handler)
public Form(String name, String method, String actionURL)
Log.getDefault()
and the default SubmitHackedHandler
public void destroy()
public Object remove(Field field)
Map.remove(Object)
for the general contract.field
- the field to remove from the form.public Form add(Field field)
Note, field names have to be unique. "Grouped" fields (such
as HTML form checkboxes or radio buttons) which require the
same field name are encapsulated by a single appropriate
grouped field object such as RadioGroup
and CheckboxGroup
. This field object itself should have a
unique name.
IllegalArgumentException
- if an element with the same name
already exists in this form.public Form add(Dependency d)
public Form add(FieldRef ref)
FieldRef
to this Form. This is useful when
to track a set of related fields in a form. (Note: those fields
must also directly be added to the form).public void addError(FormData fd, String errorKey, String errorMessage)
FormValidator
(these classes are used outside of the
validation framework and their error message still needs to be added to
the form data).
This is a very useful method for adding the results of arbitrary code to this form.
fd
- form dataerrorKey
- an arbitrary/optional validation key which allows
to later retrieve and display a particular
form error. Specify null for no
key.errorMessage
- the form error messageField.addError(fc.web.forms.FormData, java.lang.String)
public void addError(FormData fd, String errorMessage)
addError
with an empty validation errorKey.public void addDynamicField(FormData fd, Hidden f)
Hidden
field to this form. This field exists only for the
duration of this form data object and is useful for request specific
data.public void addValidator(FormValidator validator)
IllegalArgumentException
- if a validator with the same name
already exists in this form.public void addMessage(FormData fd, String key, String value)
public boolean containsField(String fieldName)
public String getMessage(FormData fd, String key)
public void addSubmitHackedHandler(SubmitHackedHandler handler)
public FormData handleSubmit(javax.servlet.http.HttpServletRequest req) throws javax.servlet.ServletException, IOException
hasError(FormData)
method with the formdata returned by this method.
Seeing validation errors is optional of course, and so is using validators
in the first place.req
- the HttpServletRequest corresponding to the request
being processed by this form.FormData
object containing populated
field data and the results of the validation.javax.servlet.ServletException
IOException
public FormData handleSubmit(javax.servlet.http.HttpServletRequest req, Connection con) throws javax.servlet.ServletException, IOException
handleSubmit(HttpServletRequest)
except
it stores the specified connection in the form data. This connection can
be used by any attached dependency classes for database queries when
updating form depedencies.
Note, we can set arbitrary objects in the HttpServletRequest
via
it's setAttribute
method.
However, specifying and storing the connection in the FormData is easier
to remember and use.
There are 2 cases:
WebApp#getConnection
to obtain a connection and no connection has to be set in the form data.handleSubmit(HttpServletRequest)
can then be used.
handleSubmit(HttpServletRequest)
is sufficient.req
- the HttpServletRequest corresponding to the request
being processed by this form.con
- a connection object. The connection is not closed and
it is the callers responsiblity to do so.javax.servlet.ServletException
IOException
public FormData refresh(javax.servlet.http.HttpServletRequest req) throws javax.servlet.ServletException, IOException
javax.servlet.ServletException
IOException
public FormData refresh(javax.servlet.http.HttpServletRequest req, Connection con) throws javax.servlet.ServletException, IOException
To refresh the form for all users/requests from this point onwards, create a new instance of the form and replace the existing instance of the form with the newly created instance.
javax.servlet.ServletException
IOException
public void renderError(FormData fd, Writer writer) throws IOException
writer
- the Writer to render errors toIOException
public void renderErrorFor(FormData fd, Writer writer, String formValidatorName) throws IOException
writer
- the Writer to render errors toformValidatorName
- the name of a form level validatorIOException
public Map getFieldErrors(FormData fd)
fd
- a non-null form data objectField.getValidateErrors(fc.web.forms.FormData)
methodpublic Collection getFormErrorsCollection(FormData fd)
fd
- a non-null form data objectFormValidator.getErrorMessage()
methodpublic List getFormErrorsFor(FormData fd, String formValidatorName)
fd
- a non-null form data objectformValidatorName
- a form validator namepublic void renderDynamicFields(FormData fd, Writer out) throws SQLException, IOException
FormData
if/as needed.
Note: Dynamic fields are not part of a form's fields and are not available via
SQLException
IOException
public void setActionURL(String url)
public String getActionURL()
public void setDisplayURL(String url)
public String getDisplayURL()
public Collection getAllFields()
public Field get(String name)
name
- the name of the fieldpublic Hidden getDynamicField(FormData fd, String fieldName)
Hidden
field added to the form data object. Returns
null if the form data object is null or if the specified
field does not exist. Dyanamic fields returned by this method are only
available for the duration of the form data object and can not
obtain a field from a submitted form. (use getDynamicField(FormData, HttpServletRequest)
for that purpose.public Hidden getSubmittedField(javax.servlet.http.HttpServletRequest req, String name)
ServletRequest.getParameter(java.lang.String)
method (often the easiest
way)
#getSubmittedField(HttpServletRequest)
method (which will
attempt to read the request parameter with the specified name and return
is as a Hidden field. (this is slightly more convenient if there is more
than 1 hidden field with the same name, since all hidden fields with the
same name are encapsulated by one object of type Hidden
).
public List getContaining(String name)
name
- the name of the fieldpublic FieldRef getFieldRef(String key)
FieldRef
with the specified key. This is
used to retrieve an arbitrary object added the via #addObject
method. Returns null if no object is
found for the specified key.public Select getSelect(String name)
Select
.IllegalArgumentException
- if a field with the specified name does not exist or
if the field exists but is not of type Selectpublic RefreshableSelect getUpdatableSelect(String name)
RefreshableSelect
.IllegalArgumentException
- if a field with the specified name does not exist or
if the field exists but is not of type RefreshableSelectpublic Checkbox getCheckbox(String name)
Checkbox
.IllegalArgumentException
- if a field with the specified name does not exist or
if the field exists but is not of type Checkboxpublic CheckboxGroup getCheckboxGroup(String name)
Checkbox
.IllegalArgumentException
- if a field with the specified name does not exist or
if the field exists but is not of type Checkboxpublic Hidden getHidden(String name)
Hidden
.IllegalArgumentException
- if a field with the specified name does not exist or
if the field exists but is not of type Hiddenpublic Text getText(String name)
Text
.IllegalArgumentException
- if a field with the specified name does not exist or
if the field exists but is not of type Textpublic TextArea getTextArea(String name)
TextArea
.IllegalArgumentException
- if a field with the specified name does not exist or
if the field exists but is not of type TextAreapublic Password getPassword(String name)
Password
.IllegalArgumentException
- if a field with the specified name does not exist or
if the field exists but is not of type Passwordpublic Radio getRadio(String name)
Radio
.IllegalArgumentException
- if a field with the specified name does not exist or
if the field exists but is not of type Radiopublic RadioGroup getRadioGroup(String name)
Checkbox
.IllegalArgumentException
- if a field with the specified name does not exist or
if the field exists but is not of type Checkbox