1

I create one admin form with multiselect form. Check below my code.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$carrierOptions = $objectManager->get('\Magento\Shipping\Model\Config\Source\Allmethods')->toOptionArray();
$groupOptions = $objectManager->get('\Magento\Customer\Model\ResourceModel\Group\Collection')->toOptionArray();

        $fieldset->addField(
            'carriers',
            'multiselect',
            [
                'name'     => 'carriers[]',
                'label'    => __('Shipping Carriers'),
                'title' => __('Shipping Carriers'),
                'required' => true,
                'values' => $carrierOptions,
                'disabled' => $isElementDisabled,
            ]
        );

        $fieldset->addField(
            'customer_group',
            'multiselect',
            [
                'name'     => 'customer_group[]',
                'label'    => __('Customer Groups'),
                'title' => __('Customer Group'),
                'values' => $groupOptions,
                'disabled' => $isElementDisabled,
            ]
        );

My Save Action like this.

<?php

namespace Vendor\ShippingTest\Controller\Adminhtml\Index;

use Magento\Backend\App\Action;
use Vendor\ShippingTest\Model\ShippingTestFactory;

class Save extends \Magento\Backend\App\Action
{
    protected $ShippingTestFactory;

    public function __construct(Action\Context $context , ShippingTestFactory $ShippingTestFactory)
    {
        $this->ShippingTestFactory = $ShippingTestFactory;
        parent::__construct($context);
    }

    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('Vendor_ShippingTest::shippingtest_save');
    }

    public function execute()
    {
        $data = $this->getRequest()->getPostValue();
        $resultRedirect = $this->resultRedirectFactory->create();

        if ($data) {
            $model = $this->ShippingTestFactory->create();
            $model->setData($data)
                ->setId($this->getRequest()->getParam('id'));

            try {

                $model->save();

                $this->messageManager->addSuccess(__('ShippingTest was successfully saved'));
                $this->_objectManager->get('Magento\Backend\Model\Session')->setFormData(false);

                if ($this->getRequest()->getParam('back')) {
                    return $resultRedirect->setPath('*/*/edit', ['id' => $model->getId(), '_current' => true]);
                }
                $this->_redirect('*/*/');
                return;

            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                $this->messageManager->addError($e->getMessage());
            } catch (\RuntimeException $e) {
                $this->messageManager->addError($e->getMessage());
            } catch (\Exception $e) {
                $this->messageManager->addException($e, __($e->getMessage().'Something went wrong while saving the page.'));
            }

            $this->_getSession()->setFormData($data);
            return $resultRedirect->setPath('*/*/edit', ['id' => $this->getRequest()->getParam('id')]);
        }

        return $resultRedirect->setPath('*/*/');
    }   
}

Now when i save this form i got error like this. Notice: Array to string conversion in /vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 2915Something went wrong while saving the page. Give me proper suggestion.

3
  • Check my updated question. Commented Aug 31, 2016 at 10:41
  • You're trying to cast $value as a string for the comparison, but $value is an array. for output use var_dump($value); die; and then chk.
    – Arjun
    Commented Aug 31, 2016 at 10:41
  • Hello Arjun, I check it for your suggestion. But i can't understand. Give me proper solution. Commented Aug 31, 2016 at 10:48

1 Answer 1

3

Hello I got solution for my above question. I put two line in save action, before $model->setData($data)

$data['carriers']=implode(',',$data['carriers']);
$data['customer_group']=implode(',',$data['customer_group']);
$model->setData($data)
      ->setId($this->getRequest()->getParam('id'));

Now, no any error found and i saved this data in database.

2
  • I executed your code, But it not seems to be working for me. If you can help.
    – anonymous
    Commented Apr 12, 2018 at 9:51
  • @LazyCoder, Can you tell me what happened in your code ? Please describe me. Commented Apr 17, 2018 at 5:46

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.