how to set up a zend multiCheckbox form field with the checkboxes checked?

14,578

Solution 1

To mark certain checkboxes as checked, try this:

$multiCheckElement->setValue(array('11.com', '3.com'));

// or 

$this->addElement ( 
    'multiCheckbox', 'servers2', 
    array (
        'value' => array('11.com', '3.com'), // select these 2 values
        'multiOptions' => array(
                    '11.com' => '.com',
                    '12.com' => '12.com',
                    '16.com' => '16.com',
                    '3.com' => '17.com'
                    )
    )
);

See also Zend_Form_Element_MultiCheckbox

Solution 2

ZF2 will require you to use value_options;

$form->add(
        array(
            'name' => 'servers2',
            'type' => \Zend\Form\Element\MultiCheckbox::class,
            'attributes' => array(
                'id' => 'servers2',
                'class' => 'form-control',
            ),
            'options' => array(
                'label' => 'Servers 2',
                'column-size' => 'sm-10',
                'label_attributes' => array('class' => 'col-sm-2'),
                'twb-layout' => 'horizontal',
                'value_options' => array(
                    '11.com' => '.com',
                    '12.com' => '12.com',
                    '16.com' => '16.com',
                    '3.com' => '17.com'
                )
            ),
        )
    );
Share:
14,578
Patrioticcow
Author by

Patrioticcow

spooky action at a distance

Updated on July 26, 2022

Comments

  • Patrioticcow
    Patrioticcow almost 2 years

    i have this form:

    $this->addElement ( 
                'multiCheckbox', 'servers2', 
                array (
                       'checkedValue' => '0',
                       'multiOptions' => array(
                            '11.com' => '.com',
                            '12.com' => '12.com',
                            '16.com' => '16.com',
                            '3.com' => '17.com'
                            )
                ));
    

    the problem is that the checkedValue doesn't work for this setup, it does for a simple checkbox. I've also tried 'checkedValues' => array('1','0'), singular or plural, but no end in sight.

    any ideas?

    THanks