سؤال

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?

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top