Question

I am trying to get store selector in admin form. I created this form without using ui component. I tried the below code but it didn't worked and I got the below error.

The expected input field is:

enter image description here

The input field code is:

$fieldset->addField(
        'store_view',
        'Magento\Store\Ui\Component\Form\Field\StoreView',
        [
            'label' => __('Store view'),
            'name' => 'store_view',
            'disabled' => false,
            //'class' => 'Magento\Store\Ui\Component\Form\Field\StoreView',
            'values' => $this->options->toOptionArray()
        ]
    );

In the above code $this->options is "Magento\Cms\Ui\Component\Listing\Column\Cms\Options" class object.

The error is:

1 exception(s): Exception #0 (InvalidArgumentException): Magento\Store\Ui\Component\Form\Field\StoreView doesn't extend \Magento\Framework\Data\Form\Element\AbstractElement

Exception #0 (InvalidArgumentException): Magento\Store\Ui\Component\Form\Field\StoreView doesn't extend \Magento\Framework\Data\Form\Element\AbstractElement #1 Magento\Framework\Data\Form\AbstractForm->addField() called at [vendor/magento/framework/Data/Form/Element/Fieldset.php:214] #2 Magento\Framework\Data\Form\Element\Fieldset->addField() called at [app/code/Vital/CustomisedIntegrations/Preference/CustomisedForm.php:104] #3 Vital\CustomisedIntegrations\Preference\CustomisedForm->_addGeneralFieldset() called at [vendor/magento/module-integration/Block/Adminhtml/Integration/Edit/Tab/Info.php:53] #4 Magento\Integration\Block\Adminhtml\Integration\Edit\Tab\Info->_prepareForm() called at [vendor/magento/module-backend/Block/Widget/Form.php:156] #5 Magento\Backend\Block\Widget\Form->_beforeToHtml() called at [vendor/magento/framework/View/Element/AbstractBlock.php:1110]

Note: I need the store selector in admin form without using ui component. I found a related question here but still didn't get a proper answers. please suggest me a better solutions

Was it helpful?

Solution

Try this code

public function __construct(
    \Magento\Backend\Block\Template\Context $context,        
    \Magento\Store\Model\System\Store $systemStore,
    array $data = []
) {

    $this->_systemStore = $systemStore;
    parent::__construct($context,$data);
}


 $fieldset->addField(
   'store_id',
   'multiselect',
   [
     'name'     => 'store_id[]',
     'label'    => __('Store Views'),
     'title'    => __('Store Views'),
     'required' => true,
     'values'   => $this->_systemStore->getStoreValuesForForm(false, true),
   ]
);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top