How to disable individual options in a Zend_Form_Element_Radio
Asked Answered
M

2

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?

Methodism answered 8/2, 2010 at 12:43 Comment(0)
C
21

Yes, it is possible:

$element->setMultiOptions(array (
 'songs' => 'songs',
 'lyrics' => 'lyrics',
 'artists' => 'artists'
));
$element->setAttrib('disable', array('lyrics', 'songs'));
Clayton answered 4/3, 2010 at 1:35 Comment(4)
Thanks for the suggestion. I've not tried it out yet, but as soon as I have I'll let you know.Methodism
Confirmed in ZF 1.11.11. This answer should be accepted instead.Rhombencephalon
This answer should be accepted because it is the right one. I have tried, it works.Eyeless
My apologies for not accepting this answer earlier! Better late than never, right?Methodism
C
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);
}
Cockerel answered 24/3, 2016 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.