How to set default text field value in form Magento 2

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
5
down vote

favorite












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 MagenestMembershipBlockAdminhtmlGiftEdit;

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







share|improve this question




























    up vote
    5
    down vote

    favorite












    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 MagenestMembershipBlockAdminhtmlGiftEdit;

    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







    share|improve this question
























      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      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 MagenestMembershipBlockAdminhtmlGiftEdit;

      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







      share|improve this question














      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 MagenestMembershipBlockAdminhtmlGiftEdit;

      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









      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 6 at 6:49









      Divyarajsinh

      775




      775










      asked Sep 6 at 6:46









      Jaimin

      815326




      815326




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          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);





          share|improve this answer





























            up vote
            2
            down vote













            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());





            share|improve this answer



























              up vote
              2
              down vote













              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());





              share|improve this answer



























                up vote
                2
                down vote













                You can try the below solution as well.



                <?php
                namespace MagenestMembershipBlockAdminhtmlGiftEdit;

                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();








                share|improve this answer




















                • 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 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 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 at 9:04






                • 1




                  I just want to learn that's why I am asking you.
                  – Dinesh Yadav
                  Sep 6 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 at 9:05










                Your Answer







                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "479"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                convertImagesToLinks: false,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f241050%2fhow-to-set-default-text-field-value-in-form-magento-2%23new-answer', 'question_page');

                );

                Post as a guest






























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                5
                down vote



                accepted










                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);





                share|improve this answer


























                  up vote
                  5
                  down vote



                  accepted










                  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);





                  share|improve this answer
























                    up vote
                    5
                    down vote



                    accepted







                    up vote
                    5
                    down vote



                    accepted






                    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);





                    share|improve this answer














                    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);






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Sep 6 at 9:20

























                    answered Sep 6 at 7:19









                    Dinesh Yadav

                    3,1971730




                    3,1971730






















                        up vote
                        2
                        down vote













                        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());





                        share|improve this answer
























                          up vote
                          2
                          down vote













                          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());





                          share|improve this answer






















                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            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());





                            share|improve this answer












                            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());






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Sep 6 at 7:24









                            Manashvi Birla

                            5,82941839




                            5,82941839




















                                up vote
                                2
                                down vote













                                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());





                                share|improve this answer
























                                  up vote
                                  2
                                  down vote













                                  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());





                                  share|improve this answer






















                                    up vote
                                    2
                                    down vote










                                    up vote
                                    2
                                    down vote









                                    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());





                                    share|improve this answer












                                    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());






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Sep 6 at 8:10









                                    Ansar Husain

                                    942216




                                    942216




















                                        up vote
                                        2
                                        down vote













                                        You can try the below solution as well.



                                        <?php
                                        namespace MagenestMembershipBlockAdminhtmlGiftEdit;

                                        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();








                                        share|improve this answer




















                                        • 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 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 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 at 9:04






                                        • 1




                                          I just want to learn that's why I am asking you.
                                          – Dinesh Yadav
                                          Sep 6 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 at 9:05














                                        up vote
                                        2
                                        down vote













                                        You can try the below solution as well.



                                        <?php
                                        namespace MagenestMembershipBlockAdminhtmlGiftEdit;

                                        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();








                                        share|improve this answer




















                                        • 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 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 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 at 9:04






                                        • 1




                                          I just want to learn that's why I am asking you.
                                          – Dinesh Yadav
                                          Sep 6 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 at 9:05












                                        up vote
                                        2
                                        down vote










                                        up vote
                                        2
                                        down vote









                                        You can try the below solution as well.



                                        <?php
                                        namespace MagenestMembershipBlockAdminhtmlGiftEdit;

                                        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();








                                        share|improve this answer












                                        You can try the below solution as well.



                                        <?php
                                        namespace MagenestMembershipBlockAdminhtmlGiftEdit;

                                        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();









                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Sep 6 at 8:35









                                        Sukumar Gorai

                                        4,0662424




                                        4,0662424











                                        • 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 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 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 at 9:04






                                        • 1




                                          I just want to learn that's why I am asking you.
                                          – Dinesh Yadav
                                          Sep 6 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 at 9:05
















                                        • 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 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 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 at 9:04






                                        • 1




                                          I just want to learn that's why I am asking you.
                                          – Dinesh Yadav
                                          Sep 6 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 at 9:05















                                        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 at 9:00





                                        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 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 at 9:02





                                        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 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 at 9:04




                                        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 at 9:04




                                        1




                                        1




                                        I just want to learn that's why I am asking you.
                                        – Dinesh Yadav
                                        Sep 6 at 9:04




                                        I just want to learn that's why I am asking you.
                                        – Dinesh Yadav
                                        Sep 6 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 at 9:05




                                        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 at 9:05

















                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f241050%2fhow-to-set-default-text-field-value-in-form-magento-2%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Comments

                                        Popular posts from this blog

                                        What does second last employer means? [closed]

                                        Installing NextGIS Connect into QGIS 3?

                                        One-line joke