0

I've used below code to show category selector on custom module. Its show wihout any error. Below is code for showing category selector on form.

$fieldset->addType(
        'categories',
        '\Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Category'
);




$fieldset->addField(
        'category',
        'categories',
        [
            'name' => 'category',
            'label' => __('Category'),
            'title' => __('Category')
        ]
    );

I've save data using controller using below code as its array i need to convert it in string to save.

if ($data['category']):
  $data['category'] = implode(",",$data['category']);
endif;

its saved successfully. but when i edit data its thorow me error.

[2017-05-03 12:54:58] report.CRITICAL: exception 'Exception' with message 'Notice: Array to string conversion in /var/www/html/magento/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 2930' in /var/www/html/magento/vendor/magento/framework/App/ErrorHandler.php:61

Am i missing something here?

Update:

below is form code:

class Main extends \Magento\Backend\Block\Widget\Form\Generic implements \Magento\Backend\Block\Widget\Tab\TabInterface
{
     /**
     * @var \Magento\Cms\Model\Wysiwyg\Config
     */
    protected $_wysiwygConfig;

    /**
     * @var \Magento\Store\Model\System\Store
     */
    protected $_systemStore;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Data\FormFactory $formFactory
     * @param \Magento\Store\Model\System\Store $systemStore
     * @param \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig
     * @param array $data
     */

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        \Magento\Store\Model\System\Store $systemStore,
        \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig,
        array $data = []
    ) {
        $this->_systemStore = $systemStore;
        $this->_wysiwygConfig = $wysiwygConfig;
        parent::__construct($context, $registry, $formFactory, $data);
    }

    public function getTabLabel()
    {
        return __('Item Information');
    }


    public function getTabTitle()
    {
        return __('Item Information');
    }


    public function canShowTab()
    {
        return true;
    }


    public function isHidden()
    {
        return false;
    }

    /**
     * Prepare form before rendering HTML
     *
     * @return $this    
     */
    protected function _prepareForm()
    {
        $model = $this->_coreRegistry->registry('current_kensium_advisor_items');
        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create();
        $form->setHtmlIdPrefix('item_');
        $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Item Information')]);
        if ($model->getId()) {
            $fieldset->addField('id', 'hidden', ['name' => 'id']);
        }
        $fieldset->addType(
        'categories',
        '\Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Category'
       );

    $fieldset->addField(
            'advisor_name',
            'text',
            [
                'name' => 'advisor_name',
                'label' => __('Name'),
                'title' => __('Name'),
                'required' => true
            ]
        );

Update: I've updated code here for module.

https://github.com/kul3101/Category-Selector/tree/master/Test/Category/app/code/Test/Category

2 Answers 2

0

You need to set as an array when you edit like this

  $data['category'] = explode(",",$data['category']);
6
  • sorry. I didn't get you. I need to place it on edit.php of controller?
    – Kul
    Commented May 3, 2017 at 14:45
  • Before binding your form and data. If you still confuse then show your form code here
    – Sohel Rana
    Commented May 3, 2017 at 14:48
  • Please check i've update form code.
    – Kul
    Commented May 4, 2017 at 5:16
  • have you checked my updated question?
    – Kul
    Commented May 5, 2017 at 9:38
  • Hey @Kul need to build exact same environment then test. It will be great if you share your module or when I have enough free time to build same module.
    – Sohel Rana
    Commented May 5, 2017 at 9:44
0

I've found issue was not selector but image saving.while edit i've images data in array so now i've added condition

if(isset($_FILES['image']['name']) && $_FILES['image']['name'] != '') {
//logic
}else if (isset($data['image']['delete']) && $data['image']['delete'] == '1') {
// Logic
}else{

  $data['image'] = $data['image']['value'];
}

Your Answer

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

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