4

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

9
  • please add your save controller May 28, 2019 at 6:03
  • @RutveeSojitra, updated, please check once May 28, 2019 at 6:06
  • Are you getting multiple ids in $data for multiple select filed? have you checked? May 28, 2019 at 6:07
  • @RutveeSojitra, yes i am getting as array for those fields May 28, 2019 at 6:09
  • check if you have data passed in your $data , try print_r($data); and check what data is passed
    – fmsthird
    May 28, 2019 at 6:26

1 Answer 1

2

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);
9
  • hi @Rathod, No its not getting saved, i am getting the exception after this change,the exception is,Exception message: Notice: Array to string conversion in May 28, 2019 at 6:20
  • in controller check $data May 28, 2019 at 6:23
  • and save that field data as per array format May 28, 2019 at 6:23
  • i posted the controller data into the question, can you please updated it, how can i change the code there. May 28, 2019 at 6:27
  • please add output of $data after apply my code May 28, 2019 at 6:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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