1

OK so i have a custom module setup and it is working great except on my admin form i cannot get the multiple checkboxes to save it always just saves the last one, i did locate other posts about this but not one of them explained a method to have a simple checkbox type with multiple values.

<?php
class JRG_Sro_Block_Adminhtml_Sro_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{

    $form = new Varien_Data_Form(array(
        'id' => 'edit_form',
        'action' => $this->getUrl(
            'jrg_sro_admin/sro/edit',
            array(
                '_current' => true,
                'continue' => 0,
            )
        ),
        'method' => 'post',
    ));
    $form->setUseContainer(true);
    $this->setForm($form);


    $fieldset = $form->addFieldset(
        'general',
        array(
            'legend' => $this->__('Service Request Details')
        )
    );

    $sroDataSingleton = Mage::getSingleton(
        'jrg_sro/sro'
    );


    $this->_addFieldsToFieldset($fieldset, array(

        'sro_key' => array(
             'label' => $this->__('Service Code'),
             'input' => 'text',
             'required' => false,
             'readonly' => true,
        ),


        'phone' => array(
            'label' => $this->__('Phone'),
            'input' => 'text',
            'required' => true,
        ),

        'order_id' => array(
            'label' => $this->__('Order #'),
            'input' => 'text',
            'required' => true,
        ),

        'email_address' => array(
            'label' => $this->__('Email'),
            'input' => 'text',
            'required' => true,
        ),

        'first_name' => array(
            'label' => $this->__('First Name'),
            'input' => 'text',
            'required' => true,
        ),

        'last_name' => array(
            'label' => $this->__('Last Name'),
            'input' => 'text',
            'required' => true,
        ),

        'street_address' => array(
            'label' => $this->__('street'),
            'input' => 'text',
            'required' => true,
        ),

        'street_address_2' => array(
            'label' => $this->__('apt/suite'),
            'input' => 'text',
            'required' => false,
        ),

        'city' => array(
            'label' => $this->__('City'),
            'input' => 'text',
            'required' => true,
        ),

        'state' => array(
            'label' => $this->__('State'),
            'input' => 'text',
            'required' => true,
        ),

        'zip_code' => array(
            'label' => $this->__('Zip Code'),
            'input' => 'text',
            'required' => true,
        ),

        'warranty_services' => array(
            'label' => $this->__('Warranty Services'),
            'input' => 'checkboxes',
            'required' => true,
            'values' => array(
                        array('value'=>'1','label'=>'Tighten loose stones '),
                        array('value'=>'2','label'=>'Adjust the prongs'),
                        array('value'=>'3','label'=>'Other Please Specify'),
                   ),
            'onclick' => "",
            'onchange' => "",
            'disabled' => false,
            'after_element_html' => '<small>Comments</small>',
        ),

        'additional_services' => array(
            'label' => $this->__('Additional Services'),
            'input' => 'checkboxes',
            'required' => true,
            'values' => array(
                        array('value'=>'1','label'=>'test 1'),
                        array('value'=>'2','label'=>'test 2'),
                        array('value'=>'3','label'=>'test 3'),
                        array('value'=>'4','label'=>'test 4'),
                   ),
            'onclick' => "",
            'onchange' => "",
            'checked' => $data["additional_services"],
            'disabled' => false,
            'after_element_html' => '<small>Comments</small>',
        ),


        'ring_size' => array(
            'label' => $this->__('Ring Size To:'),
            'input' => 'text',
            'required' => true,
        ),


        /*'url_key' => array(
            'label' => $this->__('URL Key'),
            'input' => 'text',
            'required' => false,
        ),*/

        'description' => array(
            'label' => $this->__('Description'),
            'input' => 'textarea',
            'required' => true,
        ),
        'visibility' => array(
            'label' => $this->__('Status'),
            'input' => 'select',
            'required' => true,
            'options' => $sroDataSingleton->getAvailableVisibilies(),
        ),


    ));

    return $this;
}


protected function _addFieldsToFieldset(
    Varien_Data_Form_Element_Fieldset $fieldset, $fields)
{
    $requestData = new Varien_Object($this->getRequest()
        ->getPost('sro'));

    foreach ($fields as $name => $_data) {
        if ($requestValue = $requestData->getData($name)) {
            $_data['value'] = $requestValue;
        }


        $_data['name'] = "sro[$name]";

        $_data['title'] = $_data['label'];


        if (!array_key_exists('value', $_data)) {
            $_data['value'] = $this->_getBarcode()->getData($name);
        }


        $fieldset->addField($name, $_data['input'], $_data);
    }

    return $this;
}


protected function _getBarcode()
{
    if (!$this->hasData('sro')) {

        $sro = Mage::registry('current_sro');


        if (!$sro instanceof
                JRG_Sro_Model_Sro) {
            $sro = Mage::getModel(
                'jrg_sro/sro'
            );
        }

        $this->setData('sro', $sro);
    }

    return $this->getData('sro');
}

}

2

You need to specify that the checkboxes allow multiple values.
You can do that by adding a name attribute like this:

 'warranty_services' => array(
        'label' => $this->__('Warranty Services'),
        'name'  => 'warranty_services[]', //add this one and make sure it contains []
        'input' => 'checkboxes',
        'required' => true,
        'values' => array(
                    array('value'=>'1','label'=>'Tighten loose stones '),
                    array('value'=>'2','label'=>'Adjust the prongs'),
                    array('value'=>'3','label'=>'Other Please Specify'),
               ),
        'onclick' => "",
        'onchange' => "",
        'disabled' => false,
        'after_element_html' => '<small>Comments</small>',
    ), 
  • Not sure why but that is not working. Any Help Greatly appreciated. – user3931208 Oct 3 '14 at 13:55

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.