3

I made a simple form field with checkboxes

            $form = new Varien_Data_Form();
            $form->setHtmlIdPrefix('option_');
            $form->setFieldNameSuffix('option');
            $this->setForm($form);

            $values = array(
                array(
                    label => 'check1',
                    value => 'check1'
                ),
                array(
                    label => 'check2',
                    value => 'check2'
                )
            );

            $fieldset = $form->addFieldset(
                'rules',
                array('legend' => 'Rules'
            );

            $fieldset->addField(
                'checks',
                'checkboxes',
                array(
                    'name'  => 'checks',
                    'values'=> $values,
                    'after_element_html' => '<button type="button" onclick="saveAndContinueEdit()">Save</button>'
               )
            );

but when I save them, only the last(in array) checked checkbox gets saved in the database.

In my install script I have

->addColumn(
    'checks',
    Varien_Db_Ddl_Table::TYPE_TEXT, '64k',
    array(),
    'Checkbox Test'
)

Why it's not working?

2

you need to make the name of the input an array.

So instead of

$fieldset->addField(
            'checks',
            'checkboxes',
            array(
                'name'  => 'checks',
                'values'=> $values,
                'after_element_html' => '<button type="button" onclick="saveAndContinueEdit()">Save</button>'
           )
        );

use

$fieldset->addField(
            'checks',
            'checkboxes',
            array(
                'name'  => 'checks[]',
                'values'=> $values,
                'after_element_html' => '<button type="button" onclick="saveAndContinueEdit()">Save</button>'
           )
        );
|improve this answer|||||
  • It saves 'Array' in the database now. But I feel this is the right approach. Thanks – mpfmon Jul 16 '15 at 10:52
  • 2
    That's a totally different issue. You need to get the value send via post for this field in your save action and convert it to string. You can get the value like $this->getRequest()->getPost('checks');. And just implode(',', $value);. And when on edit mode you need to explode the existing values before passing them to the form. – Marius Jul 16 '15 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.