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.