I want to set field value of form field in Magento 2.
Expected Result
I have added required code here which are available in my Form.php
<?php
namespace Magenest\Membership\Block\Adminhtml\Gift\Edit;
class Form extends FormGeneric
{
protected $_status;
protected $_systemStore;
protected $_wysiwygConfig;
protected $productGift;
public function __construct(
Context $context,
Registry $registry,
FormFactory $formFactory,
GroupRepositoryInterface $groupRepository,
SearchCriteriaBuilder $searchCriteriaBuilder,
DataObject $objectConverter,
SystemStore $systemStore,
Config $wysiwygConfig,
ProductGift $productGift,
array $data = []
) {
$this->_systemStore = $systemStore;
$this->_groupRepository = $groupRepository;
$this->_searchCriteriaBuilder = $searchCriteriaBuilder;
$this->_objectConverter = $objectConverter;
$this->_wysiwygConfig = $wysiwygConfig;
$this->productGift = $productGift;
parent::__construct($context, $registry, $formFactory, $data);
}
protected function _prepareForm()
{
$model = $this->_coreRegistry->registry('membership_gift_edit');
$data = $model->getData();
$form = $this->_formFactory->create();
$form->setHtmlIdPrefix('page_');
$fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Gift')." ".$model->getId()]);
if ($model->getId()) {
$fieldset->addField(
'id',
'hidden',
['name' => 'id']
);
}
$fieldset->addField(
'customer_id',
'text',
[
'label' => __('Customer Id'),
'title' => __('Customer Id'),
'name' => 'customer_id',
'required' => true,
]
);
$form->setUseContainer(true);
$form->setAction($this->getUrl('membership/gift/save'));
$form->setMethod('post');
$form->setUseContainer(true);
$form->setId('edit_form');
$form->setValues($model->getData());
$this->setForm($form);
return parent::_prepareForm();
}
}
I have tried as below but didn't get result.
Try 1:
$fieldset->addField(
'customer_id',
'text',
[
'label' => __('Customer Id'),
'title' => __('Customer Id'),
'name' => 'customer_id',
'required' => true,
]
);
$fieldset->setValue('myvalue');
Try 2:
$fieldset->addField(
'customer_id',
'text',
[
'label' => __('Customer Id'),
'title' => __('Customer Id'),
'name' => 'customer_id',
'value' => 'test',
]
);
I also refer below link but still facing an issue. How to set field value of form field in Magento 2