Question

I am creating a multicheckbox and filling it afterwards with a loop. Is it possible to disable one of the checkboxes afterwards or during the filling?

$multiOptions = array();
$multiCheckbox = new Zend_Form_Element_MultiCheckbox('multi_name', $multiOptions);
foreach($valuesArray AS $value) {
    $name = $this->getName($value);
    $multiCheckbox->addMultiOption($name, $value);
}

I tryed to disable one checkbox by code below, but it did not work. Anyone got better suggestions?

$multiCheckbox->setAttrib('disable', $this->getName($valuesArray[2]));

this did not help either:

$multiCheckbox->setAttrib('disable', $valuesArray[2]);

The array with values is just a simple array:

$valuesArray(1, 2, 3, 4, 5);
Était-ce utile?

La solution

You will want to create a new view helper to so that you can set the disabled attribute of the element when it is displayed.

The current view helper code looks like this:

public function formMultiCheckbox($name, $value=null, $attribs=null, $options=null, $listsep="<br />\n") {
    return $this->formRadio($name, $value, $attribs, $options, $listsep);
}

You should be able to set an 'disabledElement' option when you are creating your multicheckbox and then in your view helper check if the checkbox being displayed should be disabled or not.

public function formMultiCheckbox($name, $value=null, $attribs=null, $options=null, $listsep="<br />\n") {
    $disableElements = $options['disableElement']; 

    if(in_array($name, $disableElements, true)) {
        $options['disable'] => true;
    }

    return $this->formRadion($name, $value, $attribs, $options, $listsep);
}

Overriding the view helper to check which elements should be disabled and then setting the option should work.

You may want to extend the Form_Element_MultiCheckbox into your own class and creating the view helper for that element yourself. Based on the Zend Framework manual

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top