net.sf.click.control
Class Field

java.lang.Object
  extended bynet.sf.click.control.AbstractControl
      extended bynet.sf.click.control.Field
All Implemented Interfaces:
Control, Serializable
Direct Known Subclasses:
Button, Checkbox, FieldSet, FileField, HiddenField, Label, Radio, RadioGroup, Select, TextArea, TextField

public abstract class Field
extends AbstractControl

Provides an abstract form Field control. Field controls are contained by the Form control which will orchestrate the processing and rendering of the contained fields. All Form field controls must extend this abstract class.

Field Processing

Post Requests

When processing POST requests forms typically invoke the onProcess() method on all its fields. The Field onProcess() method is used to bind the fields request value, validate the submission and invoke any control listener method. If the onProcess() method returns true the form will continue processing fields, otherwise the form will abort further processing.

The body of the Field onProcess() method is detailed below.

 public boolean onProcess() {
     bindRequestValue();

     if (getValidate()) {
         validate();
     }

     registerActionEvent();

     return true;
 } 
The Field methods called by onProcess() include:
bindRequestValue()
This method will bind the HTTP request value to the Field's value.
getValidate()
This method will return true if the Field should validate itself. This value is generally inherited from the parent Form, however the Field can override this value and specify whether it should be validated.
validate()
This method will validate the submitted Field value. If the submitted value is not valid this method should set the Field error property, which can be rendered by the Form.
AbstractControl.registerActionEvent()
This method will register any Control action listener method which has be defined for the Field.
Field subclasses generally only have to override the validate() method, and possibly the bindRequestValue() method, to provide their own behaviour.

Get Requests

When processing GET requests a Page's Form will typically perform no processing and simply render itself and its Fields.

Rendering

Field subclasses must override the Object.toString() method to enable themselves to be rendered as HTML. With the increasing use of AJAX, Fields should render themselves as valid XHTML, so that they may be parsed correctly and used as the innerHtml in the DOM.

When a Form object renders a Field using autolayout, it renders the Field in a table row using the Field's label attribute, its error attribute if defined, and the Fields toString() method.

To assist with rendering valid HTML Field subclasses can use the HtmlStringBuffer class.

Message Resources

Fields support a hierarchy of resource bundles for displaying validation error messages and display messages. These localized messages can be accessed through the methods: The order in which localized messages resolve are:
Page scope messages
Message lookups are first resolved to the Pages message bundle if it exists. For example a Login page may define the message properties:
 /com/mycorp/page/Login.properties 
If you want messages to be used only for a specific Page, this is where to place them.
Global page scope messages
Next message lookups are resolved to the global pages message bundle if it exists.
 /click-page.properties 
If you want messages to be used across your entire application this is where to place them.
Control scope messages
Next message lookups are resolved to the Control message bundle if it exists. For example a CustomTextField control may define the message properties:
 /com/mycorp/control/CustomTextField.properties 
Global control scope messages
Finally message lookups are resolved to the global application control message bundle if the message has not already found. The global control properties file is:
 /click-control.properties 
You can modify these properties by copying this file into your applications root class path and editing these properties.

Note when customizing the message properties you must include all the properties, not just the ones you want to override.

Author:
Malcolm Edgar
See Also:
Serialized Form

Field Summary
protected  boolean disabled
          The Field disabled value.
protected  String error
          The Field error message.
protected  boolean focus
          The request focus flag.
protected  Form form
          The parent Form.
protected  String help
          The Field help text.
protected  String label
          The Field label.
protected  boolean readonly
          The Field is readonly flag.
protected  boolean required
          The Field is required flag.
protected  int tabindex
          The Field 'tabindex' attribute.
protected  String title
          The Field 'title' attribute, which acts as a tooltip help message.
protected  Boolean validate
          The validate Field value onProcess() invokation flag.
protected  String value
          The Field value.
 
Fields inherited from class net.sf.click.control.AbstractControl
actionListener, attributes, listener, listenerMethod, messages, name, parent, styles
 
Fields inherited from interface net.sf.click.Control
CONTROL_MESSAGES
 
Constructor Summary
Field()
          Construct a new Field object.
Field(String name)
          Construct the Field with the given name.
Field(String name, String label)
          Construct the Field with the given name and label.
 
Method Summary
 void bindRequestValue()
          This method binds the submitted request value to the Field's value.
 String getError()
          Return the validation error message if the Field is not valid, or null if valid.
protected  String getErrorLabel()
          Return a normalized label for display in error messages.
 boolean getFocus()
          Return true if the field has requested focus.
 String getFocusJavaScript()
          Return the Field focus JavaScript.
 Form getForm()
          Return the parent Form containing the Field or null if no form is present in the parent hierarchy.
 String getHelp()
          Return the field help text.
 String getId()
          Return the Form and Field id appended:   "form-field"

Use the field the "id" attribute value if defined, or the name otherwise.

 String getLabel()
          Return the field display label.
protected  String getRequestValue()
          Return the field's value from the request.
 int getTabIndex()
          Return the field "tabindex" attribute value.
 String getTextAlign()
          Return the field CSS "text-align" style, or null if not defined.
 String getTitle()
          Return the 'title' attribute, or null if not defined.
 boolean getValidate()
          Return true if the Field should validate itself when being processed.
 String getValidationJavaScript()
          Return the field JavaScript client side validation function.
 String getValue()
          Return the Field value.
 Object getValueObject()
          Return the object representation of the Field value.
 String getWidth()
          Return the width CSS "width" style, or null if not defined.
 boolean isDisabled()
          Return true if the Field is disabled.
 boolean isHidden()
          Return true if the Field type is hidden (<input type="hidden"/>) or false otherwise.
 boolean isReadonly()
          Return true if the Field is a readonly.
 boolean isRequired()
          Return true if the Field's value is required.
 boolean isValid()
          Return true if the Field is valid after being processed, or false otherwise.
 void onInit()
          This method does nothing.
 boolean onProcess()
          This method processes the page request returning true to continue processing or false otherwise.
 void setDisabled(boolean disabled)
          Set the Field disabled flag.
 void setError(String error)
          Set the Field validation error message.
protected  void setErrorMessage(String key)
          Set the error with the a label formatted message specified by the given message bundle key.
protected  void setErrorMessage(String key, double value)
          Set the error with the a label and value formatted message specified by the given message bundle key.
protected  void setErrorMessage(String key, int value)
          Set the error with the a label and value formatted message specified by the given message bundle key.
protected  void setErrorMessage(String key, Object value)
          Set the error with the a label and value formatted message specified by the given message bundle key.
 void setFocus(boolean focus)
          Set the Field request focus flag.
 void setForm(Form form)
          Set the Field's the parent Form.
 void setHelp(String help)
          Set the Field help text.
 void setLabel(String label)
          Set the Field display caption.
 void setListener(Object listener, String method)
          The callback listener will only be called during processing if the Field value is valid.
 void setParent(Object parent)
          Set the parent of the Field.
 void setReadonly(boolean readonly)
          Set the Field readonly flag.
 void setRequired(boolean required)
          Set the Field required status.
 void setTabIndex(int tabindex)
          Set the field "tabindex" attribute value.
 void setTextAlign(String align)
          Set the field CSS horizontal "text-align" style.
 void setTitle(String value)
          Set the 'title' attribute tooltip message.
 void setValidate(boolean validate)
          Set the validate Field value when being processed flag.
 void setValue(String value)
          Set the Field value.
 void setValueObject(Object object)
          Set the value of the field using the given object.
 void setWidth(String value)
          Set the the CSS "width" style attribute.
 void validate()
          The validate method is invoked by onProcess() to validate the request submission.
 
Methods inherited from class net.sf.click.control.AbstractControl
addStyleClass, appendAttributes, getActionListener, getAttribute, getAttributes, getContext, getControlSizeEst, getHtmlImports, getMessage, getMessage, getMessage, getMessages, getName, getPage, getParent, getStyle, getStyles, getTag, hasAttribute, hasAttributes, hasStyles, onDeploy, onDestroy, onRender, registerActionEvent, removeStyleClass, render, renderTagBegin, renderTagEnd, setActionListener, setAttribute, setId, setName, setStyle, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

disabled

protected boolean disabled
The Field disabled value.


error

protected String error
The Field error message.


focus

protected boolean focus
The request focus flag.


form

protected Form form
The parent Form.


help

protected String help
The Field help text.


label

protected String label
The Field label.


readonly

protected boolean readonly
The Field is readonly flag.


required

protected boolean required
The Field is required flag.


tabindex

protected int tabindex
The Field 'tabindex' attribute.


title

protected String title
The Field 'title' attribute, which acts as a tooltip help message.


validate

protected Boolean validate
The validate Field value onProcess() invokation flag.


value

protected String value
The Field value.

Constructor Detail

Field

public Field()
Construct a new Field object.


Field

public Field(String name)
Construct the Field with the given name.

Parameters:
name - the name of the Field

Field

public Field(String name,
             String label)
Construct the Field with the given name and label.

Parameters:
name - the name of the Field
label - the label of the Field
Method Detail

setParent

public void setParent(Object parent)
Set the parent of the Field.

Specified by:
setParent in interface Control
Overrides:
setParent in class AbstractControl
Parameters:
parent - the parent of the Control
Throws:
IllegalStateException - if AbstractControl.name is not defined
IllegalArgumentException - if the given parent instance is referencing this object: if (parent == this)
See Also:
Control.setParent(Object)

isDisabled

public boolean isDisabled()
Return true if the Field is disabled. The Field will also be disabled if the parent FieldSet or Form is disabled.

Important Note: disabled fields will not submit their values in a HTML form POST. This may cause validation issues in a form submission. Please note this is a HTML limitation and is not due to Click.

Returns:
true if the Field is disabled

setDisabled

public void setDisabled(boolean disabled)
Set the Field disabled flag.

Important Note: disabled fields will not submit their values in a HTML form POST. This may cause validation issues in a form submission. Please note this is a HTML limitation and is not due to Click.

Parameters:
disabled - the Field disabled flag

getError

public String getError()
Return the validation error message if the Field is not valid, or null if valid.

Returns:
the Field validation error message, or null if valid

setError

public void setError(String error)
Set the Field validation error message. If the error message is not null the Field is invalid, otherwise it is valid.

Parameters:
error - the validation error message

getFocus

public boolean getFocus()
Return true if the field has requested focus.

Returns:
true if the field has requested focus

setFocus

public void setFocus(boolean focus)
Set the Field request focus flag.

Parameters:
focus - the request focus flag

getFocusJavaScript

public String getFocusJavaScript()
Return the Field focus JavaScript.

Returns:
the Field focus JavaScript

getForm

public Form getForm()
Return the parent Form containing the Field or null if no form is present in the parent hierarchy.

Returns:
the parent Form containing the Field

setForm

public void setForm(Form form)
Set the Field's the parent Form.

Parameters:
form - Field's parent Form

getHelp

public String getHelp()
Return the field help text.

If the help value is null, this method will attempt to find a localized help message in the parent messages using the key:

getName() + ".help"
If not found then the message will be looked up in the /click-control.properties file using the same key.

For examle given a CustomerPage with the properties file CustomerPage.properties:

 name.label=Customer Name
 name.help=Full name or Business name 
The page TextField code:
 public class CustomerPage extends Page {
     TextField nameField = new TextField("name");
     ..
 } 
Will render the TextField label and title properties as:
 <td><label>Customer Name</label></td>
 <td><input type="text" name="name"/> <span="Full name or Business name"/>/td> 
How the help text is rendered is depends upon the Field subclass.

Returns:
the help text of the Field

setHelp

public void setHelp(String help)
Set the Field help text.

Parameters:
help - the help text of the Field

isHidden

public boolean isHidden()
Return true if the Field type is hidden (<input type="hidden"/>) or false otherwise. By default this method returns false.

Returns:
false

getId

public String getId()
Return the Form and Field id appended:   "form-field"

Use the field the "id" attribute value if defined, or the name otherwise.

Specified by:
getId in interface Control
Overrides:
getId in class AbstractControl
Returns:
HTML element identifier attribute "id" value
See Also:
Control.getId()

getLabel

public String getLabel()
Return the field display label.

If the label value is null, this method will attempt to find a localized label message in the parent messages using the key:

getName() + ".label"
If not found then the message will be looked up in the /click-control.properties file using the same key. If a value is still not found, the Field name will be converted into a label using the method: ClickUtils.toLabel(String)

For example given a CustomerPage with the properties file CustomerPage.properties:

 name.label=Customer Name
 name.title=Full name or Business name 
The page TextField code:
 public class CustomerPage extends Page {
     TextField nameField = new TextField("name");
     ..
 } 
Will render the TextField label and title properties as:
 <td><label>Customer Name</label></td>
 <td><input type="text" name="name" title="Full name or Business name"/></td> 
When a label value is not set, or defined in any properties files, then its value will be created from the Fields name.

For example given the TextField code:

 TextField nameField = new TextField("faxNumber");  
Will render the TextField label as:
 <td><label>Fax Number</label></td>
 <td><input type="text" name="faxNumber"/></td> 

Returns:
the display label of the Field

setLabel

public void setLabel(String label)
Set the Field display caption.

Parameters:
label - the display label of the Field

setListener

public void setListener(Object listener,
                        String method)
The callback listener will only be called during processing if the Field value is valid. If the field has validation errors the listener will not be called.

Specified by:
setListener in interface Control
Overrides:
setListener in class AbstractControl
Parameters:
listener - the listener object with the named method to invoke
method - the name of the method to invoke
See Also:
Control.getName()

isReadonly

public boolean isReadonly()
Return true if the Field is a readonly. The Field will also be readonly if the parent FieldSet or Form is readonly.

Returns:
true if the Field is a readonly

setReadonly

public void setReadonly(boolean readonly)
Set the Field readonly flag.

Parameters:
readonly - the Field readonly flag

isRequired

public boolean isRequired()
Return true if the Field's value is required.

Returns:
true if the Field's value is required

setRequired

public void setRequired(boolean required)
Set the Field required status.

Parameters:
required - set the Field required status

getTabIndex

public int getTabIndex()
Return the field "tabindex" attribute value.

Returns:
the field "tabindex" attribute value

setTabIndex

public void setTabIndex(int tabindex)
Set the field "tabindex" attribute value.

Parameters:
tabindex - the field "tabindex" attribute value

getTextAlign

public String getTextAlign()
Return the field CSS "text-align" style, or null if not defined.

Returns:
the field CSS "text-align" style, or null if not defined.

setTextAlign

public void setTextAlign(String align)
Set the field CSS horizontal "text-align" style.

Parameters:
align - the CSS "text-align" value: ["left", "right", "center"]

getTitle

public String getTitle()
Return the 'title' attribute, or null if not defined. The title attribute acts like tooltip message over the Field.

If the title value is null, this method will attempt to find a localized label message in the parent messages using the key:

getName() + ".title"
If not found then the message will be looked up in the /click-control.properties file using the same key. If still not found the title will be left as null and will not be rendered.

For examle given a CustomerPage with the properties file CustomerPage.properties:

 name.label=Customer Name
 name.title=Full name or Business name 
The page TextField code:
 public class CustomerPage extends Page {
     TextField nameField = new TextField("name");
     ..
 } 
Will render the TextField label and title properties as:
 <td><label>Customer Name</label></td>
 <td><input type="text" name="name" title="Full name or Business name"/></td> 

Returns:
the 'title' attribute tooltip message

setTitle

public void setTitle(String value)
Set the 'title' attribute tooltip message.

Parameters:
value - the 'title' attribute tooltip message

getValidate

public boolean getValidate()
Return true if the Field should validate itself when being processed.

If the validate attribute for the Field is not explicity set, this method will return the validation status of its parent Form, see Form.getValidate(). If the Field validate attribute is not set and the parent Form is not set this method will return true.

This method is called by the onProcess() method to determine whether the the Field validate() method should be invoked.

Returns:
true if the Field should validate itself when being processed.

setValidate

public void setValidate(boolean validate)
Set the validate Field value when being processed flag.

Parameters:
validate - the field value when processed

getValidationJavaScript

public String getValidationJavaScript()
Return the field JavaScript client side validation function.

The function name must follow the format validate_[id], where the id is the DOM element id of the fields focusable HTML element, to ensure the function has a unique name.

Returns:
the field JavaScript client side validation function

isValid

public boolean isValid()
Return true if the Field is valid after being processed, or false otherwise. If the Field has no error message after Control.onProcess() has been invoked it is considered to be valid.

Returns:
true if the Field is valid after being processed

getValue

public String getValue()
Return the Field value.

Returns:
the Field value

setValue

public void setValue(String value)
Set the Field value.

Parameters:
value - the Field value

getValueObject

public Object getValueObject()
Return the object representation of the Field value. This method will return a string value, or null if the string value is null or is zero length.

Specialized object field subclasses should override this method to return a non string object. For examples a DoubleField would return a Double value instead.

Returns:
the object representation of the Field value

setValueObject

public void setValueObject(Object object)
Set the value of the field using the given object.

Parameters:
object - the object value to set

getWidth

public String getWidth()
Return the width CSS "width" style, or null if not defined.

Returns:
the CSS "width" style attribute, or null if not defined

setWidth

public void setWidth(String value)
Set the the CSS "width" style attribute.

Parameters:
value - the CSS "width" style attribute

bindRequestValue

public void bindRequestValue()
This method binds the submitted request value to the Field's value.


onInit

public void onInit()
This method does nothing. Subclasses may override this method to perform additional initialization.

Specified by:
onInit in interface Control
Overrides:
onInit in class AbstractControl
See Also:
Control.onInit()

onProcess

public boolean onProcess()
This method processes the page request returning true to continue processing or false otherwise. The Field onProcess() method is typically invoked by the Form onProcess() method when processing POST request.

This method will bind the Field request parameter value to the field, validate the submission and invoke its callback listener if defined. The code of this method is provided below:

 public boolean onProcess() {
     bindRequestValue();

     if (getValidate()) {
         validate();
     }

     registerActionEvent();

     return true
 } 

Specified by:
onProcess in interface Control
Overrides:
onProcess in class AbstractControl
Returns:
true to continue Page event processing or false otherwise
See Also:
Control.onProcess()

validate

public void validate()
The validate method is invoked by onProcess() to validate the request submission. Field subclasses should override this method to implement request validation logic.

If the field determines that the submission is invalid it should set error property with the error message.


getErrorLabel

protected String getErrorLabel()
Return a normalized label for display in error messages.

The error label is a normalized version of getLabel().

Returns:
a normalized label for error message display

setErrorMessage

protected void setErrorMessage(String key)
Set the error with the a label formatted message specified by the given message bundle key. The message will be formatted with the field label using getErrorLabel().

setErrorMessage will attempt to find a localized error message as described here, using the following lookup strategy:

Parameters:
key - the key of the localized message bundle string

setErrorMessage

protected void setErrorMessage(String key,
                               Object value)
Set the error with the a label and value formatted message specified by the given message bundle key. The message will be formatted with the field label {0} using getErrorLabel() and the given value {1}.

Also see setErrorMessage(java.lang.String) on how to specify error messages for specific Field instances.

Parameters:
key - the key of the localized message bundle string
value - the value to format in the message

setErrorMessage

protected void setErrorMessage(String key,
                               int value)
Set the error with the a label and value formatted message specified by the given message bundle key. The message will be formatted with the field label {0} using getErrorLabel() and the given value {1}.

Also see setErrorMessage(java.lang.String) on how to specify error messages for specific Field instances.

Parameters:
key - the key of the localized message bundle string
value - the value to format in the message

setErrorMessage

protected void setErrorMessage(String key,
                               double value)
Set the error with the a label and value formatted message specified by the given message bundle key. The message will be formatted with the field label {0} using getErrorLabel() and the given value {1}.

Also see setErrorMessage(java.lang.String) on how to specify error messages for specific Field instances.

Parameters:
key - the key of the localized message bundle string
value - the value to format in the message

getRequestValue

protected String getRequestValue()
Return the field's value from the request.

Returns:
the field's value from the request