Magento 2 – Multiselect Form Field Not Saved to Database

adminformmagento2select

I have created a custom admin form.

$fieldset->addField(
    'core_units',
        'multiselect',
        [
            'name' => 'core_units[]',
            'label' => __('Select Core units'),
            'title' => __('Select Core units'),
            'class' => 'core_units',
            'values' => [
                         ["value" => 1,"label" => __("Option 1")],
                        ["value" => 2,"label" => __("Option 2")],
                      ["value" => 3,"label" => __("Option 3")],
                     ["value" => 4,"label" => __("Option 4")],
                      ["value" => 5,"label" => __("Option 5")],
                       ]
        ]
    );

    $fieldset->addField(
    'optional_units',
        'multiselect',
        [
            'name' => 'optional_units[]',
            'label' => __('Select Optional units'),
            'title' => __('Select Optional units'),
            'class' => 'optional_units',
            'values' => [
            ["value" => 1,"label" => __("Option 1")],
            ["value" => 2,"label" => __("Option 2")],
            ["value" => 3,"label" => __("Option 3")],
            ["value" => 4,"label" => __("Option 4")],
            ["value" => 5,"label" => __("Option 5")],
                        ]
        ]
    );

Here is my Save controller file code.

class Save extends \Magento\Backend\App\Action
{

public function execute()
{

    $data = $this->getRequest()->getParams();
    if ($data) {
        $model = $this->_objectManager->create('X247commerce\Qualbuilder\Model\Qualbuilder');

        $id = $this->getRequest()->getParam('id');
        if ($id) {
            $model->load($id);
        }

        $model->setData($data);

        try {
            $model->save();
            $this->messageManager->addSuccess(__('The Frist Grid Has been Saved.'));
            $this->_objectManager->get('Magento\Backend\Model\Session')->setFormData(false);
            if ($this->getRequest()->getParam('back')) {
                $this->_redirect('*/*/edit', array('id' => $model->getId(), '_current' => true));
                return;
            }
            $this->_redirect('*/*/');
            return;
        } catch (\Magento\Framework\Model\Exception $e) {
            $this->messageManager->addError($e->getMessage());
        } catch (\RuntimeException $e) {
            $this->messageManager->addError($e->getMessage());
        } catch (\Exception $e) {
            $this->messageManager->addException($e, __('Something went wrong while saving the banner.'));
        }

        $this->_getSession()->setFormData($data);
        $this->_redirect('*/*/edit', array('banner_id' => $this->getRequest()->getParam('banner_id')));
        return;
    }
    $this->_redirect('*/*/');
   }
}

The above code showing the multi select drop down in the form, but whenever options selected and saved, its not getting saved to database.

I am posting the controller data.

 Array
(
 [key] => 4317ff2ff34444a61eac0c70b0502924318cb0465c3c7244b0e59eef5c593059

[form_key] => ndLnj7PmwQfgbKYG
[core_units] => Array
    (
        [0] => 1
        [1] => 3
    )

[optional_units] => Array
    (
        [0] => 1
        [1] => 3
    )

)

If same row re-opened again. the options are non selected.

can anyone look into this and update me your thoughts.

Thanks in advance

Best Answer

Change Your Field Name :-

From :-

'name' => 'core_units[]'

To :-

'name' => 'core_units'

And Also 2nd Field :-

From :-

'name' => 'optional_units[]',

To :-

'name' => 'optional_units',

In Controller :-

$data['core_units']=implode(',',$data['core_units']);
$data['optional_units']=implode(',',$data['optional_units']);
$model->setData($data);