1

I created a custom module with a multiselect type input.

My InstallSchema.php file contains this for this field.

->addColumn( 'elegible_products', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 500, ['nullable => false'], 'Products' ) 

My Main.php file looks like

$fieldset->addField( 'elegible_products', 'multiselect', [ 'label' => __('Products'), 'title' => __('Products'), 'name' => 'elegible_products', 'values' => \Ayakil\FreeGift\Block\Adminhtml\Freegifts\Grid::getValueArray4(), 'disabled' => $isElementDisabled ] ); 

This is my Grid.php file

static public function getOptionArray4() { $data_array = array(); $data_array[0] = 'a1'; $data_array[1] = 'a2'; return($data_array); } static public function getValueArray4() { $data_array = array(); foreach(\Ayakil\FreeGift\Block\Adminhtml\Freegifts\Grid::getOptionArray4() as $k => $v) { $data_array[] = array('value' => $k, 'label' => $v); } return($data_array); } 

If i tried to save the form i am getting an error like below.

{"0":"Notice: Array to string conversion in /home/muja/www/ayakil-greens/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 3105","1"

How can add Multiselect in custom admin form and save the data and retrieve the data here?

0

1 Answer 1

3

It seems like in your save controller, you need to set multiselect's value as a string. For eg :

$data['elegible_products'] = implode(',',$data['elegible_products']); 

And then, you need to save records.

Hope, It will helpful for you.

2
  • welcome :) Happy coding !! Commented Dec 31, 2019 at 5:42
  • After this, I am facing a new issue when I try to see the saved values for multi-select. When I see the form, the selected values are not visible. Commented May 2, 2023 at 10:17

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.