PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Metadata and Attributes

While a form's usefulness primarily derives from the elements it contains, it can also contain other metadata, such as a name (often used as a unique ID in the HTML markup); the form action and method; the number of elements, groups, and sub forms it contains; and arbitrary metadata (usually used to set HTML attributes for the form tag itself).

You can set and retrieve a form's name using the name accessors:

<?php
// Set the name:
$form->setName('registration');

// Retrieve the name:
$name $form->getName();

To set the action (url to which the form submits) and method (method by which it should submit, e.g., 'POST' or 'GET'), use the action and method accessors:

<?php
// Set the action and method:
$form->setAction('/user/login')
     ->
setMethod('post');

You may also specify the form encoding type specifically using the enctype accessors. Zend_Form defines two constants, Zend_Form::ENCTYPE_URLENCODED and Zend_Form::ENCTYPE_MULTIPART, corresponding to the values 'application/x-www-form-urlencoded' and 'multipart/form-data', respectively; however, you can set this to any arbitrary encoding type.

<?php
// Set the action, method, and enctype:
$form->setAction('/user/login')
     ->
setMethod('post')
     ->
setEnctype(Zend_Form::ENCTYPE_MULTIPART);

Note

The method, action, and enctype are only used internally for rendering, and not for any sort of validation.

Zend_Form implements the Countable interface, allowing you to pass it as an argument to count:

<?php
$numItems 
count($form);

Setting arbitrary metadata is done through the attribs accessors. Since overloading in Zend_Form is used to access elements, display groups, and sub forms, this is the only method for accessing metadata.

<?php
// Setting attributes:
$form->setAttrib('class''zend-form')
     ->
addAttribs(array(
         
'id'       => 'registration',
         
'onSubmit' => 'validate(this)',
     ));

// Retrieving attributes:
$class $form->getAttrib('class');
$attribs $form->getAttribs();

// Remove an attribute:
$form->removeAttrib('onSubmit');

// Clear all attributes:
$form->clearAttribs();

Zend Framework