How to create multi-select type input in magento 2 custom form?

Usually in our custom module we have custom form to enter data. We use text box, Select, Text-area etc in our forms. Recently i faced an issue while creating the form with multi-select input type. So i will write this article to explain how to create a multi-select type input type and save the data.

Create the field – Main.php ( app/code/Ayakil/FreeGift/Block/Adminhtml/Freegifts/Edit/Tab/Main.php )

Create the getOptionArray4 function – Grid.php ( app/code/Ayakil/FreeGift/Block/Adminhtml/Freegifts/Grid.php )

With these after i run the setup:upgrade. If i tried to add the data, 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”

For this we want to edit the save controller( app/code/Eight25media/FreeGift/Controller/Adminhtml/freegifts/Save.php ) with following code. Want to implode the values and set the data to save.

That’s it , many articles covered how to create a multi-select but i thought to include the error while saving the form and solution with this article,Hope this may help some one’s time in future.

That’s it. Have a nice day.Enjoy coding , Learn , Experience , Teach and Help.

Leave a Reply

Your email address will not be published. Required fields are marked *