Zend\Form\Element

Zend\Form\Element is a base class for all specialized elements and Zend\Form\Fieldset, but can also be used for all generic text, select, radio, etc. type form inputs which do not have a specialized element available.

Basic Usage of Zend\Form\Element

At the bare minimum, each element or fieldset requires a name. You will also typically provide some attributes to hint to the view layer how it might render the item.

 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\Element;
use Zend\Form\Form;

$username = new Element('username');
$username
    ->setLabel('Username');
    ->setAttributes(array(
        'type'  => 'text',
        'class' => 'username',
        'size'  => '30',
    ));

$password = new Element('password');
$password
    ->setLabel('Password');
    ->setAttributes(array(
        'type'  => 'password',
        'size'  => '30',
    ));

$form = new Form('my-form');
$form
    ->add($username)
    ->add($password);

Available Methods

setName

setName(string $name)

Set the name for this element.

Returns Zend\Form\Element

getName

getName()

Return the name for this element.

Returns string

setLabel

setLabel(string $label)

Set the label content for this element.

Returns Zend\Form\Element

getLabel

getLabel()

Return the label content for this element.

Returns string

setLabelAttributes

setLabelAttributes(array $labelAttributes)

Set the attributes to use with the label.

Returns Zend\Form\Element

getLabelAttributes

getLabelAttributes()

Return the attributes to use with the label.

Returns array

setOptions

setOptions(array $options)

Set options for an element. Accepted options are: "label" and "label_attributes", which call setLabel and setLabelAttributes, respectively.

Returns Zend\Form\Element

setAttribute

setAttribute(string $key, mixed $value)

Set a single element attribute.

Returns Zend\Form\Element

getAttribute

getAttribute(string $key)

Retrieve a single element attribute.

Returns mixed

hasAttribute

hasAttribute(string $key)

Check if a specific attribute exists for this element.

Returns boolean

setAttributes

setAttributes(array|Traversable $arrayOrTraversable)

Set many attributes at once. Implementation will decide if this will overwrite or merge.

Returns Zend\Form\Element

getAttributes

getAttributes()

Retrieve all attributes at once.

Returns array|Traversable

clearAttributes

clearAttributes()

Clear all attributes for this element.

Returns Zend\Form\Element

setMessages

setMessages(array|Traversable $messages)

Set a list of messages to report when validation fails.

Returns Zend\Form\Element

setMessages

getMessages()

Returns a list of validation failure messages, if any.

Returns array|Traversable