Disable element or option in Zend_Form_Element

When disabling an element or option in a Zend_Form we use the function setAttrib() or, if multiple options, setAttribs(). These examples are made in Zend Framework 1.10

Disable a form element in the controller

$form = new Application_Form_MyForm();
$form->getElement('elementName')->setAttrib('disable', 'disable');

This will disable the whole ‘elementName’ element.

Disable option in a radio or select element
In declaration of element:

class Application_Form_MyForm extends Zend_Form {
...
        $typeOfLap = new Zend_Form_Element_Radio('elementName');
        $typeOfLap->setRequired(true)
                ->addMultiOptions(array(
                    'A' => 'Option A',
                    'B' => 'Option B'));
...
}

In controller

$form = new Application_Form_MyForm();
$form->getElement('elementName')->setAttrib('disable', array('A'));

This will disable option ‘Option A’. If you want to also disable ‘Option B’ you can add it to the disable array: array(‘A’, ‘B’). It works the same way with select elements

  1. Awesome, thanks for the detailed explanation! I was struggling to find any documentation on this so thank you for sharing 😆

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

This site uses Akismet to reduce spam. Learn how your comment data is processed.