Zend Framework ships with concrete element classes covering most HTML form elements. Most simply specify a particular view helper for use when decorating the element, but several offer additional functionality. The following is a list of all such classes, as well as descriptions of the functionality they offer.
Used for creating HTML button elements,
Zend_Form_Element_Button
extends Zend_Form_Element_Submit,
deriving its custom functionality. It specifies the 'formButton'
view helper for decoration.
Like the submit element, it uses the element's label as the element value for display purposes; in other words, to set the text of the button, set the value of the element. The label will be translated if a translation adapter is present.
Because the label is used as part of the element, the button element uses only the ViewHelper and DtDdWrapper decorators.
After populating or validating a form, you can check if the given
button was clicked using the isChecked()
method.
HTML checkboxes allow you return a specific value, but basically operate as booleans: when it is checked, the value is submitted; when it's not checked, nothing is submitted. Internally, Zend_Form_Element_Checkbox enforces this state.
By default, the checked value is '1', and the unchecked value '0'.
You can specify the values to use using the
setCheckedValue()
and setUncheckedValue()
accessors, respectively. Internally, any time you set the value, if
the provided value matches the checked value, then it is set, but
any other value causes the unchecked value to be set.
Additionally, setting the value sets the checked
property of the checkbox. You can query this using
isChecked()
or simply accessing the property. Using the
setChecked($flag)
method will both set the state of the
flag as well as set the approriate checked or unchecked value in the
element. Please use this method when setting the checked state of a
checkbox element to ensure the value is set properly.
Zend_Form_Element_Checkbox
uses the 'formCheckbox' view
helper. The checked value is always used to populate it.
Hidden elements merely inject data that should be submitted, but
which the user should not manipulate.
Zend_Form_Element_Hidden
accomplishes this through use
of the 'formHidden' view helper.
This element provides protection from CSRF attacks on forms, ensuring the data is submitted by the user session that generated the form and not by a rogue script. Protection is achieved by adding a hash element to a form and verifying it when the form is submitted.
The name of the hash element should be unique. It is recommended to use
the salt
option for the element, two hashes with
same names and different salts would not collide:
<?php $form->addElement('hash', 'no_csrf_foo', array('salt' => 'unique')); ?>
You can set the salt later using the setSalt($salt)
method.
Internally, the element stores a unique identifier using
Zend_Session_Namespace
, and checks for it at
submission (checking that the TTL has not expired). The 'Identical'
validator is then used to ensure the submitted hash matches the
stored hash.
The 'formHidden' view helper is used to render the element in the form.
Images can be used as form elements, and allow you to specify graphical elements as form buttons.
Images need an image source. Zend_Form_Element_Image
allows you to specify this by using the setImage()
accessor (or 'image' configuration key). You can also optionally specify a value to use when
submitting the image using the setImageValue()
accessor
(or 'imageValue' configuration key). When the value set for the
element matches the imageValue
, then the accessor
isChecked()
will return true.
The Image element uses the
Image
Decorator for rendering (as well as the standard Errors,
HtmlTag, and Label decorators). You can optionally specify a tag to
the Image
decorator that will then wrap the image
element.
Often you have a set of related checkboxes, and you wish to group the results. This is much like a Multiselect, but instead of them being in a dropdown list, you need to show checkbox/value pairs.
Zend_Form_Element_MultiCheckbox
makes this a snap. Like
all other elements extending the base Multi element, you can specify
a list of options, and easily validate against that same list. The
'formMultiCheckbox' view helper ensures that these are returned as
an array in the form submission.
You may manipulate the various checkbox options using the following methods:
addMultiOption($option, $value)
addMultiOptions(array $options)
setMultiOptions(array $options)
(overwrites existing options)getMultiOption($option)
getMultiOptions()
removeMultiOption($option)
clearMultiOptions()
XHTML select
elements allow a 'multiple' attribute,
indicating multiple options may be selected for submission, instead
of the usual one. Zend_Form_Element_Multiselect
extends
Zend_Form_Element_Select,
and sets the multiple
attribute to 'multiple'. Like
other classes that inherit from the base
Zend_Form_Element_Multi
class, you can manipulate the
options for the select using:
addMultiOption($option, $value)
addMultiOptions(array $options)
setMultiOptions(array $options)
(overwrites existing options)getMultiOption($option)
getMultiOptions()
removeMultiOption($option)
clearMultiOptions()
If a translation adapter is registered with the form and/or element, option values will be translated for display purposes.
Password elements are basically normal text elements -- except that you typically do not want the submitted password displayed in error messages or the element itself when the form is re-displayed.
Zend_Form_Element_Password
achieves this by calling
setObscureValue(true)
on each validator (ensuring that
the password is obscured in validation error messages), and using
the 'formPassword' view helper (which does not display the value
passed to it).
Radio elements allow you to specify several options, of which you
need a single value returned. Zend_Form_Element_Radio
extends the base Zend_Form_Element_Multi
class,
allowing you to specify a number of options, and then uses the
formRadio
view helper to display these.
Like all elements extending the Multi element base class, the following methods may be used to manipulate the radio options displayed:
addMultiOption($option, $value)
addMultiOptions(array $options)
setMultiOptions(array $options)
(overwrites existing options)getMultiOption($option)
getMultiOptions()
removeMultiOption($option)
clearMultiOptions()
Reset buttons are typically used to clear a form, and are not part of submitted data. However, as they serve a purpose in the display, they are included in the standard elements.
Zend_Form_Element_Reset
extends Zend_Form_Element_Submit.
As such, the label is used for the button display, and will be
translated if a translation adapter is present. It utilizes only the
'ViewHelper' and 'DtDdWrapper' decorators, as there should never be
error messages for such elements, nor will a label be necessary.
Select boxes are a common way of limiting to specific choices for a
given form datum. Zend_Form_Element_Select
allows you
to generate these quickly and easily.
As it extends the base Multi element, the following methods may be used to manipulate the select options:
addMultiOption($option, $value)
addMultiOptions(array $options)
setMultiOptions(array $options)
(overwrites existing options)getMultiOption($option)
getMultiOptions()
removeMultiOption($option)
clearMultiOptions()
Zend_Form_Element_Select
uses the 'formSelect' view
helper for decoration.
Submit buttons are used to submit a form. You may use multiple
submit buttons; you can use the button used to submit the form to
decide what action to take with the data submitted.
Zend_Form_Element_Submit
makes this decisioning easy,
by adding a isChecked()
method; as only one button
element will be submitted by the form, after populating or
validating the form, you can call this method on each submit button
to determine which one was used.
Zend_Form_Element_Submit
uses the label as the "value"
of the submit button, translating it if a translation adapter is
present. isChecked()
checks the submitted value against
the label in order to determine if the button was used.
The ViewHelper and DtDdWrapper decorators to render the element. No label decorator is used, as the button label is used when rendering the element; also, typically, you will not associate errors with a submit element.
By far the most prevalent type of form element is the text element,
allowing for limited text entry; it's an ideal element for most data
entry. Zend_Form_Element_Text
simply uses the
'formText' view helper to display the element.
Textareas are used when large quantities of text are expected, and
place no limits on the amount of text submitted (other than maximum
size limits as dictated by your server or PHP).
Zend_Form_Element_Textarea
uses the 'textArea' view
helper to display such elements, placing the value as the content of
the element.