2

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

asked May 4, 2021 at 16:58

1 Answer 1

4

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),
 ]
);
answered May 5, 2021 at 4:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.