Magento – Magento 2 Form Field Array (Advance system configuration option)

magento2

Recently I worked on advance system config form field in Magento 2. I checked Braintree payment method for reference.

When I saved a value, it showing following error:

Something went wrong while saving this configuration: Notice: Array to
string conversion in
E:\wamp\www\magento2\app\code\Magento\Config\Model\Resource\Config\Data.php
on line 38

After print $object->getValue() array is printed with error:

screenshot 1

and

screenshot 2

(click screenshots to enlarge)

Best Answer

Add backend model for this config path. Something like

<backend_model>NameSpace\Module\Model\Backend\Config</backend_model>

And backend model class looks like

namespace NameSpace\Module\Model\Backend
class Config  extends \Magento\Framework\App\Config\Value
{
    public function beforeSave()
    {
        $data = $this->getValue();
        if (is_array($data)) {
            $this->setValue(implode(',', $data));
        }
    }
}
Related Topic