9

Is it possible to disable individual options in a Zend_Form_Element_Radio? That is, I'd like to add disabled="disabled" to certain input tags.

Does the Zend Framework include this functionality? Or is there another way to accomplish this?

2 Answers2

21

Yes, it is possible:

$element->setMultiOptions(array (
 'songs' => 'songs',
 'lyrics' => 'lyrics',
 'artists' => 'artists'
));
$element->setAttrib('disable', array('lyrics', 'songs'));
Sampson
  • 265,109
  • 74
  • 539
  • 565
ProfLogic
  • 234
  • 2
  • 2
  • Thanks for the suggestion. I've not tried it out yet, but as soon as I have I'll let you know. –  Mar 20 '10 at 12:48
  • 1
    Confirmed in ZF 1.11.11. This answer should be accepted instead. – LinusR Oct 15 '12 at 22:05
  • This answer should be accepted because it is the right one. I have tried, it works. – darpet Mar 04 '13 at 15:04
  • 1
    My apologies for not accepting this answer earlier! Better late than never, right? –  Mar 24 '14 at 12:59
0

It works best on the option-key. Here is a function for disabling every option except the currently active one:

/**
 * This function disables all options of the given selectElement, except for the active one
 * @param \Zend_Form_Element_Select $selectElement
 * @throws \Zend_Form_Exception
 */
private function disableAllOtherOptions(\Zend_Form_Element_Select $selectElement)
{
    $theOneAndOnlyActiveValue = $selectElement->getValue();
    $optionsToDisable = [];
    foreach ($selectElement->options as $key => $option) {
        if ($key <> $theOneAndOnlyActiveValue) {
            $optionsToDisable[] = $key;
        }
    }
    $selectElement->setAttrib('disable', $optionsToDisable);
}