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.

3
  • 2
    thank you, This helped me.
    – Mujahidh
    Dec 31, 2019 at 5:41
  • welcome :) Happy coding !! 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. May 2 at 10:17

Your Answer

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

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