5

I want to set field value of form field in Magento 2.

Actual Result enter image description here

Expected Result

enter image description here

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

8

Try this

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

$model->setData('customer_id', '10');

Second way:

You can achieve this using following way also.

$fieldset->addField(
    'customer_id',
    'text',
    [
        'label'    => __('Customer Id'),
        'title'    => __('Customer Id'),
        'name'     => 'customer_id',
        'required' => true,
        'value' => '10',
    ]
);

...
...

    if ($model->getId()) {
        $form->setValues($model->getData());
    }
    $this->setForm($form);
3

setValues() function is overriding the 'value' that you have set for the field. This line of code which would reset the form values:

$form->setValues($model->getData());

Solution:

Set model data before setValues() function as below:

if (!$model->getId()) {
    $model->setData('customer_id', '19');
}
$form->setValues($model->getData());
2

Use below code on same file it will sure work.

$model->setData('customer_id', "20");
 $form->setValues($model->getData());

I did use below code and its work fine for me

enter image description here

$model->setData('title', "yourValue");
  $form->setValues($model->getData());
2

You can try the below solution as well.

<?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,
                    'value' => '19'
                ]
            );


        $form->setUseContainer(true);
        $form->setAction($this->getUrl('membership/gift/save'));
        $form->setMethod('post');
        $form->setUseContainer(true);
        $form->setId('edit_form');
        $_data = $model->getData();
        if ($model->getId()) {
            $form->setValues($_data);
        }
        $this->setForm($form);
        return parent::_prepareForm();
    }

}
  • Can you please explain what did you do to show the value? Because he has already tried your solution with `'value' => 'test' but it didn't work for him. Is there anything else you did? – Dinesh Yadav Sep 6 '18 at 9:00
  • Did you check the whole code? I have also put condition just before setForm. Don't comment without checking the whole code. – Sukumar Gorai Sep 6 '18 at 9:02
  • Yes I saw that but he already did it. He tried with $form->setValues($model->getData()); and you just took the $model->getData(); in variable and then set it. – Dinesh Yadav Sep 6 '18 at 9:04
  • 1
    I just want to learn that's why I am asking you. – Dinesh Yadav Sep 6 '18 at 9:04
  • Okay I got it. You are seting the data only if the model has id. otherwise you are not seting the data. – Dinesh Yadav Sep 6 '18 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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