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

Zend_Form_Element handles a variety of attributes and element metadata. Basic attributes include:

  • name: the element name. Uses the setName() and getName() accessors.

  • label: the element label. Uses the setLabel() and getLabel() accessors.

  • order: the index at which an element should appear in the form. Uses the setOrder() and getOrder() accessors.

  • value: the current element value. Uses the setValue() and getValue() accessors.

  • description: a description of the element; often used to provide tooltip or javascript contextual hinting describing the purpose of the element. Uses the setDescription() and getDescription() accessors.

  • required: flag indicating whether or not the element is required when performing form validation. Uses the setRequired() and getRequired() accessors. This flag is FALSE by default.

  • allowEmpty: flag indicating whether or not a non-required (optional) element should attempt to validate empty values. If it is set to TRUE and the required flag is FALSE, empty values are not passed to the validator chain and are presumed TRUE. Uses the setAllowEmpty() and getAllowEmpty() accessors. This flag is TRUE by default.

  • autoInsertNotEmptyValidator: flag indicating whether or not to insert a 'NotEmpty' validator when the element is required. By default, this flag is TRUE. Set the flag with setAutoInsertNotEmptyValidator($flag) and determine the value with autoInsertNotEmptyValidator().

Form elements may require additional metadata. For XHTML form elements, for instance, you may want to specify attributes such as the class or id. To facilitate this are a set of accessors:

  • setAttrib($name, $value): add an attribute

  • setAttribs(array $attribs): like addAttribs(), but overwrites

  • getAttrib($name): retrieve a single attribute value

  • getAttribs(): retrieve all attributes as key/value pairs

Most of the time, however, you can simply access them as object properties, as Zend_Form_Element utilizes overloading to facilitate access to them:

// Equivalent to $element->setAttrib('class', 'text'):
$element->class 'text;

By default, all attributes are passed to the view helper used by the element during rendering, and rendered as HTML attributes of the element tag.

Zend Framework