0

I am creating a custom fom in admin and want to load the custom select option in the form as a select field options.

Below is my code :

app/code/Vendor/Module/Block/Adminhtml/Grid/Edit/Form.php

<?php
namespace Vendor\Module\Block\Adminhtml\Grid\Edit;
 
 
/**
 * Adminhtml Add New Row Form.
 */
class Form extends \Magento\Backend\Block\Widget\Form\Generic
{
 /**
 * @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 array $data
 */
 public function __construct(
 \Magento\Backend\Block\Template\Context $context,
 \Magento\Framework\Registry $registry,
 \Magento\Framework\Data\FormFactory $formFactory,
 \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig,
 \Vendor\Module\Model\Status $options,
 array $data = []
 ) 
 {
 $this->_options = $options;
 $this->_wysiwygConfig = $wysiwygConfig;
 parent::__construct($context, $registry, $formFactory, $data);
 }
 
 /**
 * Prepare form.
 *
 * @return $this
 */
 protected function _prepareForm()
 {
 
 $dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
 $model = $this->_coreRegistry->registry('row_data');
 $form = $this->_formFactory->create(
 ['data' => [
 'id' => 'edit_form', 
 'enctype' => 'multipart/form-data', 
 'action' => $this->getData('action'), 
 'method' => 'post'
 ]
 ]
 );
 
 $form->setHtmlIdPrefix('question_');
 
 $fieldset->addField(
 'name',
 'text',
 [
 'name' => 'name',
 'label' => __('NAME'),
 'id' => 'name',
 'title' => __('NAME'),
 'class' => 'required-entry',
 'required' => true,
 ]
 );
 $fieldset->addField(
 'email',
 'text',
 [
 'name' => 'email',
 'label' => __('Email'),
 'id' => 'email',
 'title' => __('Email'),
 'class' => 'required-entry',
 'required' => true,
 ]
 );
 
 $fieldset->addField('is_answered', 'select', 
 [
 'name' => 'is_answered',
 'label' => __('Status'),
 'options' => ['1' => __('Pending By Seller'), '0' => __('Pending By Admin')],
 'required' => true,
 ]);
 $form->setValues($model->getData());
 $form->setUseContainer(true);
 $this->setForm($form);
 
 return parent::_prepareForm();
 }
}

I want to load custom data(email) entries from custom table in the email field as a drop-down in the form.

asked Jul 5, 2021 at 14:04

2 Answers 2

1

Try with below code

Create source provider for email values

Vendor\Module\Model\Email\Options.php

<?php
namespace Vendor\Module\Model\Email;
class Options implements \Magento\Framework\Option\ArrayInterface
{ 
 /**
 * Return array of options as value-label pairs, eg. value => label
 *
 * @return array
 */
 public function toOptionArray()
 {
 // You can write your code to fetch email values from custom table and convert it to as value => label pair
 return [
 'value' => 'Label',
 '[email protected]' => '[email protected]',
 '[email protected]' => '[email protected]',
 ];
 }
}

Vendor\Module\Block\Adminhtml\Grid\Edit\Form.php

<?php
namespace Vendor\Module\Block\Adminhtml\Grid\Edit;
/**
 * Adminhtml Add New Row Form.
 */
class Form extends \Magento\Backend\Block\Widget\Form\Generic
{
 /**
 * @var \Magento\Store\Model\System\Store
 */
 protected $_systemStore;
 /**
 * @var \Vendor\Module\Model\Email\Options
 */
 protected $_emailOptions;
 /**
 * @param \Magento\Backend\Block\Template\Context $context
 * @param \Magento\Framework\Registry $registry
 * @param \Magento\Framework\Data\FormFactory $formFactory
 * @param array $data
 */
 public function __construct(\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Data\FormFactory $formFactory, \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig, \Vendor\Module\Model\Status $options, \Vendor\Module\Model\Email\Options $emailOptions, array $data = [])
 {
 $this->_options = $options;
 $this->_wysiwygConfig = $wysiwygConfig;
 $this->_emailOptions = $emailOptions;
 parent::__construct($context, $registry, $formFactory, $data);
 }
 /**
 * Prepare form.
 *
 * @return $this
 */
 protected function _prepareForm()
 {
 $dateFormat = $this
 ->_localeDate
 ->getDateFormat(\IntlDateFormatter::SHORT);
 $model = $this
 ->_coreRegistry
 ->registry('row_data');
 $form = $this
 ->_formFactory
 ->create(['data' => ['id' => 'edit_form', 'enctype' => 'multipart/form-data', 'action' => $this->getData('action') , 'method' => 'post']]);
 $form->setHtmlIdPrefix('question_');
 $fieldset->addField(
 'name',
 'text',
 [
 'name' => 'name',
 'label' => __('NAME'),
 'id' => 'name',
 'title' => __('NAME'),
 'class' => 'required-entry',
 'required' => true,
 ]
 );
 $fieldset->addField(
 'email',
 'select',
 [
 'name' => 'email',
 'label' => __('Email'),
 'id' => 'email',
 'title' => __('Email'),
 'values' => $this->_emailOptions->toOptionArray() 'class' => 'select', 'required' => true, ]);
 $fieldset->addField('is_answered', 'select', 
 [
 'name' => 'is_answered',
 'label' => __('Status'),
 'options' => ['1' => __('Pending By Seller'), '0' => __('Pending By Admin')],
 'required' => true,
 ]);
 $form->setValues($model->getData());
 $form->setUseContainer(true);
 $this->setForm($form);
 return parent::_prepareForm();
 }
}

Thanks!

answered Jul 5, 2021 at 14:52
0

You can add select field like this :

$fieldset->addField(
 'custom_dropdown',
 'select',
 [
 'name' => 'custom_dropdown',
 'label' => __('Custom Dropdown'),
 'title' => __('Custom Dropdown'),
 'values' => [0 => __('No'), 1 => __('Yes')],
 'class' => 'select'
 ]
);

Replace custom_dropdown value by your field name & add your custom data array in values field.

answered Jul 5, 2021 at 14:10
4
  • This will work if we have only a few options but in my case there are number entries in that custom table. it won't be possible to write them all here in this file. I have tried this but by doing this the values of these option comes as 1,2,3,4... etc. and i want them to be same as select options. Commented Jul 5, 2021 at 14:17
  • You can add like this : $this->attributeSetOptions->toOptionArray(); Here toOptionArray() with return custom data. Commented Jul 5, 2021 at 14:18
  • I am a bit confused can you explain it. how actually i can do this. Commented Jul 5, 2021 at 14:20
  • vendor/magento/module-configurable-product/Block/Adminhtml/Product/Edit/AttributeSet/Form.php here you can see that how dynamic value set as I told you. Let me know still if you have confusion. Commented Jul 5, 2021 at 14:29

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.