Zend Framework comes with an initial set of helper classes related to Forms: e.g., rendering a text input, selection box, or form labels. You can use helper, or plugin, classes to perform these behaviors for you.
See the section on view helpers for more information.
The Form view helper is used to render a <form> HTML element and its attributes.
Basic usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | use Zend\Form\Form;
use Zend\Form\Element;
// Within your view...
$form = new Form();
// ...add elements and input filter to form...
// Set attributes
$form->setAttribute('action', $this->url('contact/process'));
$form->setAttribute('method', 'post');
// Prepare the form elements
$form->prepare();
// Render the opening tag
echo $this->form()->openTag($form);
// <form action="/contact/process" method="post">
// ...render the form elements...
// Render the closing tag
echo $this->form()->closeTag();
// </form>
|
The following public methods are in addition to those inherited from Zend\Form\View\Helper\AbstractHelper.
Renders the <form> open tag for the $form instance.
Return type: | string |
---|
Renders a </form> closing tag.
Return type: | string |
---|
The FormButton view helper is used to render a <button> HTML element and its attributes.
Basic usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | use Zend\Form\Element;
$element = new Element\Button('my-button');
$element->setLabel("Reset");
// Within your view...
/**
* Example #1: Render entire button in one shot...
*/
echo $this->formButton($element);
// <button name="my-button" type="button">Reset</button>
/**
* Example #2: Render button in 3 steps
*/
// Render the opening tag
echo $this->formButton()->openTag($element);
// <button name="my-button" type="button">
echo '<span class="inner">' . $element->getLabel() . '</span>';
// Render the closing tag
echo $this->formButton()->closeTag();
// </button>
/**
* Example #3: Override the element label
*/
echo $this->formButton()->render($element, 'My Content');
// <button name="my-button" type="button">My Content</button>
|
Renders the <button> open tag for the $element instance.
Return type: | string |
---|
Renders a </button> closing tag.
Return type: | string |
---|
Renders a button’s opening tag, inner content, and closing tag.
Parameters: |
|
---|---|
Return type: | string |
TODO
Basic usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 | use Zend\Captcha;
use Zend\Form\Element;
$captcha = new Element\Captcha('captcha');
$captcha
->setCaptcha(new Captcha\Dumb())
->setLabel('Please verify you are human');
// Within your view...
echo $this->formCaptcha($captcha);
// TODO
|
The FormCheckbox view helper can be used to render a <input type="checkbox"> HTML form input. It is meant to work with the Zend\Form\Element\Checkbox element, which provides a default input specification for validating the checkbox values.
FormCheckbox extends from Zend\Form\View\Helper\FormInput.
Basic usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | use Zend\Form\Element;
$element = new Element\Checkbox('my-checkbox');
// Within your view...
/**
* Example #1: Default options
*/
echo $this->formCheckbox($element);
// <input type="hidden" name="my-checkbox" value="0">
// <input type="checkbox" name="my-checkbox" value="1">
/**
* Example #2: Disable hidden element
*/
$element->setUseHiddenElement(false);
echo $this->formCheckbox($element);
// <input type="checkbox" name="my-checkbox" value="1">
/**
* Example #3: Change checked/unchecked values
*/
$element->setUseHiddenElement(true)
->setUncheckedValue('no')
->setCheckedValue('yes');
echo $this->formCheckbox($element);
// <input type="hidden" name="my-checkbox" value="no">
// <input type="checkbox" name="my-checkbox" value="yes">
|
The FormElement view helper proxies the rendering to specific form view helpers depending on the type of the Zend\Form\Element that is passed in. For instance, if the passed in element had a type of “text”, the FormElement helper will retrieve and use the FormText helper to render the element.
Basic usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | use Zend\Form\Form;
use Zend\Form\Element;
// Within your view...
/**
* Example #1: Render different types of form elements
*/
$textElement = new Element\Text('my-text');
$checkboxElement = new Element\Checkbox('my-checkbox');
echo $this->formElement($textElement);
// <input type="text" name="my-text" value="">
echo $this->formElement($checkboxElement);
// <input type="hidden" name="my-checkbox" value="0">
// <input type="checkbox" name="my-checkbox" value="1">
/**
* Example #2: Loop through form elements and render them
*/
$form = new Form();
// ...add elements and input filter to form...
$form->prepare();
// Render the opening tag
echo $this->form()->openTag($form);
// ...loop through and render the form elements...
foreach ($form as $element) {
echo $this->formElement($element); // <-- Magic!
echo $this->formElementErrors($element);
}
// Render the closing tag
echo $this->form()->closeTag();
|
The FormElementErrors view helper is used to render the validation error messages of an element.
Basic usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | use Zend\Form\Form;
use Zend\Form\Element;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Input;
// Create a form
$form = new Form();
$element = new Element\Text('my-text');
$form->add($element);
// Create a input
$input = new Input('my-text');
$input->setRequired(true);
$inputFilter = new InputFilter();
$inputFilter->add($input);
$form->setInputFilter($inputFilter);
// Force a failure
$form->setData(array()); // Empty data
$form->isValid(); // Not valid
// Within your view...
/**
* Example #1: Default options
*/
echo $this->formElementErrors($element);
// <ul><li>Value is required and can't be empty</li></ul>
/**
* Example #2: Add attributes to open format
*/
echo $this->formElementErrors($element, array('class' => 'help-inline'));
// <ul class="help-inline"><li>Value is required and can't be empty</li></ul>
/**
* Example #3: Custom format
*/
echo $this->formElementErrors()
->setMessageOpenFormat('<div class="help-inline">')
->setMessageSeparatorString('</div><div class="help-inline">')
->setMessageCloseString('</div>')
->render($element);
// <div class="help-inline">Value is required and can't be empty</div>
|
The following public methods are in addition to those inherited from Zend\Form\View\Helper\AbstractHelper.
Set the formatted string used to open message representation.
Parameters: | $messageOpenFormat – The formatted string to use to open the messages. Uses '<ul%s><li>' by default. Attributes are inserted here. |
---|
Returns the formatted string used to open message representation.
Return type: | string |
---|
Sets the string used to separate messages.
Parameters: | $messageSeparatorString – The string to use to separate the messages. Uses '</li><li>' by default. |
---|
Returns the string used to separate messages.
Return type: | string |
---|
Sets the string used to close message representation.
Parameters: | $messageCloseString – The string to use to close the messages. Uses '</li></ul>' by default. |
---|
Returns the string used to close message representation.
Return type: | string |
---|
Set the attributes that will go on the message open format.
Parameters: | $attributes – Key value pairs of attributes. |
---|
Returns the attributes that will go on the message open format.
Return type: | array |
---|
Renders validation errors for the provided $element.
Parameters: |
|
---|---|
Return type: | string |
The FormFile view helper can be used to render a <input type="file"> form input. It is meant to work with the Zend\Form\Element\File element.
FormFile extends from Zend\Form\View\Helper\FormInput.
Basic usage:
1 2 3 4 5 6 7 8 | use Zend\Form\Element;
$element = new Element\File('my-file');
// Within your view...
echo $this->formFile($element);
// <input type="file" name="my-file">
|
For HTML5 multiple file uploads, the multiple attribute can be used. Browsers that do not support HTML5 will default to a single upload input.
1 2 3 4 5 6 7 8 9 | use Zend\Form\Element;
$element = new Element\File('my-file');
$element->setAttribute('multiple', true);
// Within your view...
echo $this->formFile($element);
// <input type="file" name="my-file" multiple="multiple">
|
The FormImage view helper can be used to render a <input type="image"> HTML form input. It is meant to work with the Zend\Form\Element\Image element.
FormImage extends from Zend\Form\View\Helper\FormInput.
Basic usage:
1 2 3 4 5 6 7 8 9 | use Zend\Form\Element;
$element = new Element\Image('my-image');
$element->setAttribute('src', '/img/my-pic.png');
// Within your view...
echo $this->formImage($element);
// <input type="image" name="my-image" src="/img/my-pic.png">
|
The FormInput view helper is used to render a <input> HTML form input tag. It acts as a base class for all of the specifically typed form input helpers (FormText, FormCheckbox, FormSubmit, etc.), and is not suggested for direct use.
It contains a general map of valid tag attributes and types for attribute filtering. Each subclass of FormInput implements it’s own specific map of valid tag attributes.
The following public methods are in addition to those inherited from Zend\Form\View\Helper\AbstractHelper.
Renders the <input> tag for the $element.
Return type: | string |
---|
The FormLabel view helper is used to render a <label> HTML element and its attributes. If you have a Zend\I18n\Translator\Translator attached, FormLabel will translate the label contents during it’s rendering.
Basic usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | use Zend\Form\Element;
$element = new Element\Text('my-text');
$element->setLabel('Label')
->setAttribute('id', 'text-id')
->setLabelAttributes(array('class' => 'control-label'));
// Within your view...
/**
* Example #1: Render label in one shot
*/
echo $this->formLabel($element);
// <label class="control-label" for="text-id">Label</label>
echo $this->formLabel($element, $this->formText($element));
// <label class="control-label" for="text-id">Label<input type="text" name="my-text"></label>
echo $this->formLabel($element, $this->formText($element), 'append');
// <label class="control-label" for="text-id"><input type="text" name="my-text">Label</label>
/**
* Example #2: Render label in separate steps
*/
// Render the opening tag
echo $this->formLabel()->openTag($element);
// <label class="control-label" for="text-id">
// Render the closing tag
echo $this->formLabel()->closeTag();
// </label>
|
Attaching a translator and setting a text domain:
1 2 3 4 5 6 7 8 | // Setting a translator
$this->formLabel()->setTranslator($translator);
// Setting a text domain
$this->formLabel()->setTranslatorTextDomain('my-text-domain');
// Setting both
$this->formLabel()->setTranslator($translator, 'my-text-domain');
|
Note
Note: If you have a translator in the Service Manager under the key, ‘translator’, the view helper plugin manager will automatically attach the translator to the FormLabel view helper. See Zend\View\HelperPluginManager::injectTranslator() for more information.
The following public methods are in addition to those inherited from Zend\Form\View\Helper\AbstractHelper.
Render a form label, optionally with content.
Always generates a “for” statement, as we cannot assume the form input will be provided in the $labelContent.
Parameters: |
|
---|---|
Return type: | string |
Renders the <label> open tag and attributes.
Parameters: | $attributesOrElement – An array of key value attributes or a ElementInterface instance. |
---|---|
Return type: | string |
Renders a </label> closing tag.
Return type: | string |
---|
The FormMultiCheckbox view helper can be used to render a group <input type="checkbox"> HTML form inputs. It is meant to work with the Zend\Form\Element\MultiCheckbox element, which provides a default input specification for validating a multi checkbox.
FormMultiCheckbox extends from Zend\Form\View\Helper\FormInput.
Basic usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | use Zend\Form\Element;
$element = new Element\MultiCheckbox('my-multicheckbox');
$element->setValueOptions(array(
'0' => 'Apple',
'1' => 'Orange',
'2' => 'Lemon',
));
// Within your view...
/**
* Example #1: using the default label placement
*/
echo $this->formMultiCheckbox($element);
// <label><input type="checkbox" name="my-multicheckbox[]" value="0">Apple</label>
// <label><input type="checkbox" name="my-multicheckbox[]" value="1">Orange</label>
// <label><input type="checkbox" name="my-multicheckbox[]" value="2">Lemon</label>
/**
* Example #2: using the prepend label placement
*/
echo $this->formMultiCheckbox($element, 'prepend');
// <label>Apple<input type="checkbox" name="my-multicheckbox[]" value="0"></label>
// <label>Orange<input type="checkbox" name="my-multicheckbox[]" value="1"></label>
// <label>Lemon<input type="checkbox" name="my-multicheckbox[]" value="2"></label>
|
The FormPassword view helper can be used to render a <input type="password"> HTML form input. It is meant to work with the Zend\Form\Element\Password element.
FormPassword extends from Zend\Form\View\Helper\FormInput.
Basic usage:
1 2 3 4 5 6 | use Zend\Form\Element;
$element = new Element\Password('my-password');
// Within your view...
echo $this->formPassword($element);
|
Output:
1 | <input type="password" name="my-password" value="">
|
The FormRadio view helper can be used to render a group <input type="radio"> HTML form inputs. It is meant to work with the Zend\Form\Element\Radio element, which provides a default input specification for validating a radio.
FormRadio extends from Zend\Form\View\Helper\FormMultiCheckbox.
Basic usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | use Zend\Form\Element;
$element = new Element\Radio('gender');
$element->setValueOptions(array(
'0' => 'Male',
'1' => 'Female',
));
// Within your view...
/**
* Example #1: using the default label placement
*/
echo $this->formRadio($element);
// <label><input type="radio" name="gender[]" value="0">Male</label>
// <label><input type="radio" name="gender[]" value="1">Female</label>
/**
* Example #2: using the prepend label placement
*/
echo $this->formRadio($element, 'prepend');
// <label>Male<input type="checkbox" name="gender[]" value="0"></label>
// <label>Female<input type="checkbox" name="gender[]" value="1"></label>
|
The FormReset view helper can be used to render a <input type="reset"> HTML form input.
FormText extends from Zend\Form\View\Helper\FormInput.
Basic usage:
1 2 3 4 5 6 7 | use Zend\Form\Element;
$element = new Element('my-reset');
$element->setAttribute('value', 'Reset');
// Within your view...
echo $this->formReset($element);
|
Output:
1 | <input type="reset" name="my-reset" value="Reset">
|
The FormSelect view helper can be used to render a group <input type="select"> HTML form input. It is meant to work with the Zend\Form\Element\Select element, which provides a default input specification for validating a select.
FormSelect extends from Zend\Form\View\Helper\FormInput.
Basic usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | use Zend\Form\Element;
$element = new Element\Select('language');
$element->setValueOptions(array(
'0' => 'French',
'1' => 'English',
'2' => 'Japanese',
'3' => 'Chinese'
));
// Within your view...
/**
* Example
*/
echo $this->formSelect($element);
|
The FormSubmit view helper can be used to render a <input type="submit"> HTML form input. It is meant to work with the Zend\Form\Element\Submit element.
FormSubmit extends from Zend\Form\View\Helper\FormInput.
Basic usage:
1 2 3 4 5 6 | use Zend\Form\Element;
$element = new Element\Submit('my-submit');
// Within your view...
echo $this->formSubmit($element);
|
Output:
1 | <input type="submit" name="my-submit" value="">
|
The FormText view helper can be used to render a <input type="text"> HTML form input. It is meant to work with the Zend\Form\Element\Text element.
FormText extends from Zend\Form\View\Helper\FormInput.
Basic usage:
1 2 3 4 5 6 | use Zend\Form\Element;
$element = new Element\Text('my-text');
// Within your view...
echo $this->formText($element);
|
Output:
1 | <input type="text" name="my-text" value="">
|
The FormTextarea view helper can be used to render a <textarea></textarea> HTML form input. It is meant to work with the Zend\Form\Element\Textarea element.
Basic usage:
1 2 3 4 5 6 | use Zend\Form\Element;
$element = new Element\Textarea('my-textarea');
// Within your view...
echo $this->formTextarea($element);
|
Output:
1 | <textarea name="my-textarea"></textarea>
|
The AbstractHelper is used as a base abstract class for Form view helpers, providing methods for validating form HTML attributes, as well as controlling the doctype and character encoding. AbstractHelper also extends from Zend\I18n\View\Helper\AbstractTranslatorHelper which provides an implementation for the Zend\I18n\Translator\TranslatorAwareInterface that allows setting a translator and text domain.
The following public methods are in addition to the inherited methods of Zend\I18n\View\Helper\AbstractTranslatorHelper.
Sets a doctype to use in the helper.
Returns the doctype used in the helper.
Return type: | string |
---|
Set the translation text domain to use in helper when translating.
Returns the character encoding used in the helper.
Return type: | string |
---|
Returns the element id. If no ID attribute present, attempts to use the name attribute. If name attribute is also not present, returns null.
Return type: | string or null |
---|
The FormColor view helper can be used to render a <input type="color"> HTML5 form input. It is meant to work with the Zend\Form\Element\Color element.
FormColor extends from Zend\Form\View\Helper\FormInput.
Basic usage:
1 2 3 4 5 6 | use Zend\Form\Element;
$element = new Element\Color('my-color');
// Within your view...
echo $this->formColor($element);
|
Output:
1 | <input type="color" name="my-color" value="">
|
The FormDate view helper can be used to render a <input type="date"> HTML5 form input. It is meant to work with the Zend\Form\Element\Date element, which provides a default input specification for validating HTML5 date values.
FormDate extends from Zend\Form\View\Helper\FormDateTime.
Basic usage:
1 2 3 4 5 6 7 8 | use Zend\Form\Element;
$element = new Element\Date('my-date');
// Within your view...
echo $this->formDate($element);
// <input type="date" name="my-date" value="">
|
The FormDateTime view helper can be used to render a <input type="datetime"> HTML5 form input. It is meant to work with the Zend\Form\Element\DateTime element, which provides a default input specification for validating HTML5 datetime values.
FormDateTime extends from Zend\Form\View\Helper\FormInput.
Basic usage:
1 2 3 4 5 6 7 8 | use Zend\Form\Element;
$element = new Element\DateTime('my-datetime');
// Within your view...
echo $this->formDateTime($element);
// <input type="datetime" name="my-datetime" value="">
|
The FormDateTimeLocal view helper can be used to render a <input type="datetime-local"> HTML5 form input. It is meant to work with the Zend\Form\Element\DateTimeLocal element, which provides a default input specification for validating HTML5 datetime values.
FormDateTimeLocal extends from Zend\Form\View\Helper\FormDateTime.
Basic usage:
1 2 3 4 5 6 7 8 | use Zend\Form\Element;
$element = new Element\DateTimeLocal('my-datetime');
// Within your view...
echo $this->formDateTimeLocal($element);
// <input type="datetime-local" name="my-datetime" value="">
|
The FormEmail view helper can be used to render a <input type="email"> HTML5 form input. It is meant to work with the Zend\Form\Element\Email element, which provides a default input specification with an email validator.
FormEmail extends from Zend\Form\View\Helper\FormInput.
Basic usage:
1 2 3 4 5 6 7 8 | use Zend\Form\Element;
$element = new Element\Email('my-email');
// Within your view...
echo $this->formEmail($element);
// <input type="email" name="my-email" value="">
|
The FormMonth view helper can be used to render a <input type="month"> HTML5 form input. It is meant to work with the Zend\Form\Element\Month element, which provides a default input specification for validating HTML5 date values.
FormMonth extends from Zend\Form\View\Helper\FormDateTime.
Basic usage:
1 2 3 4 5 6 7 8 | use Zend\Form\Element;
$element = new Element\Month('my-month');
// Within your view...
echo $this->formMonth($element);
// <input type="month" name="my-month" value="">
|
The FormSearch view helper can be used to render a <input type="search"> HTML5 form input.
FormSearch extends from Zend\Form\View\Helper\FormText.
Basic usage:
1 2 3 4 5 6 | use Zend\Form\Element;
$element = new Element('my-search');
// Within your view...
echo $this->formSearch($element);
|
Output:
1 | <input type="search" name="my-search" value="">
|
The FormTel view helper can be used to render a <input type="tel"> HTML5 form input.
FormTel extends from Zend\Form\View\Helper\FormInput.
Basic usage:
1 2 3 4 5 6 | use Zend\Form\Element;
$element = new Element('my-tel');
// Within your view...
echo $this->formTel($element);
|
Output:
1 | <input type="tel" name="my-tel" value="">
|
The FormTime view helper can be used to render a <input type="time"> HTML5 form input. It is meant to work with the Zend\Form\Element\Time element, which provides a default input specification for validating HTML5 time values.
FormTime extends from Zend\Form\View\Helper\FormDateTime.
Basic usage:
1 2 3 4 5 6 7 8 | use Zend\Form\Element;
$element = new Element\Time('my-time');
// Within your view...
echo $this->formTime($element);
// <input type="time" name="my-time" value="">
|
The FormWeek view helper can be used to render a <input type="week"> HTML5 form input. It is meant to work with the Zend\Form\Element\Week element, which provides a default input specification for validating HTML5 week values.
FormWeek extends from Zend\Form\View\Helper\FormDateTime.
Basic usage:
1 2 3 4 5 6 7 8 | use Zend\Form\Element;
$element = new Element\Week('my-week');
// Within your view...
echo $this->formWeek($element);
// <input type="week" name="my-week" value="">
|
The FormFileApcProgress view helper can be used to render a <input type="hidden" ...> with a progress ID value used by the APC File Upload Progress feature. The APC php module is required for this view helper to work. Unlike other Form view helpers, the FormFileSessionProgress helper does not accept a Form Element as a parameter.
An id attribute with a value of "progress_key" will automatically be added.
Warning
The view helper must be rendered before the file input in the form, or upload progress will not work correctly.
Best used with the Zend\ProgressBar\Upload\ApcProgress handler.
See the apc.rfc1867 ini setting in the APC Configuration documentation for more information.
FormFileApcProgress extends from Zend\Form\View\Helper\FormInput.
Basic usage:
1 2 3 4 | // Within your view...
echo $this->formFileApcProgress();
// <input type="hidden" id="progress_key" name="APC_UPLOAD_PROGRESS" value="12345abcde">
|
The FormFileSessionProgress view helper can be used to render a <input type="hidden" ...> which can be used by the PHP 5.4 File Upload Session Progress feature. PHP 5.4 is required for this view helper to work. Unlike other Form view helpers, the FormFileSessionProgress helper does not accept a Form Element as a parameter.
An id attribute with a value of "progress_key" will automatically be added.
Warning
The view helper must be rendered before the file input in the form, or upload progress will not work correctly.
Best used with the Zend\ProgressBar\Upload\SessionProgress handler.
See the Session Upload Progress in the PHP documentation for more information.
FormFileSessionProgress extends from Zend\Form\View\Helper\FormInput.
Basic usage:
1 2 3 4 | // Within your view...
echo $this->formFileSessionProgress();
// <input type="hidden" id="progress_key" name="PHP_SESSION_UPLOAD_PROGRESS" value="12345abcde">
|
The FormFileUploadProgress view helper can be used to render a <input type="hidden" ...> which can be used by the PECL uploadprogress extension. Unlike other Form view helpers, the FormFileUploadProgress helper does not accept a Form Element as a parameter.
An id attribute with a value of "progress_key" will automatically be added.
Warning
The view helper must be rendered before the file input in the form, or upload progress will not work correctly.
Best used with the Zend\ProgressBar\Upload\UploadProgress handler.
See the PECL uploadprogress extension for more information.
FormFileUploadProgress extends from Zend\Form\View\Helper\FormInput.
Basic usage:
1 2 3 4 | // Within your view...
echo $this->formFileSessionProgress();
// <input type="hidden" id="progress_key" name="UPLOAD_IDENTIFIER" value="12345abcde">
|
The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.