How to set default text field value in form Magento 2
Clash 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
Expected Result
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
magento2 admin adminform
add a comment |Â
up vote
5
down vote
favorite
I want to set field value of form field in Magento 2.
Actual Result
Expected Result
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
magento2 admin adminform
add a comment |Â
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
Expected Result
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
magento2 admin adminform
I want to set field value of form field in Magento 2.
Actual Result
Expected Result
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
magento2 admin adminform
magento2 admin adminform
edited Sep 6 at 6:49
Divyarajsinh
1196
1196
asked Sep 6 at 6:46
Jaimin
852326
852326
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
7
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);
add a comment |Â
up vote
3
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());
add a comment |Â
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
$model->setData('title', "yourValue");
$form->setValues($model->getData());
add a comment |Â
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();
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
 |Â
show 5 more comments
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
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);
add a comment |Â
up vote
7
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);
add a comment |Â
up vote
7
down vote
accepted
up vote
7
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);
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);
edited Sep 6 at 9:20
answered Sep 6 at 7:19
Dinesh Yadav
3,3021731
3,3021731
add a comment |Â
add a comment |Â
up vote
3
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());
add a comment |Â
up vote
3
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());
add a comment |Â
up vote
3
down vote
up vote
3
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());
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());
answered Sep 6 at 7:24
Manashvi Birla
5,85941839
5,85941839
add a comment |Â
add a comment |Â
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
$model->setData('title', "yourValue");
$form->setValues($model->getData());
add a comment |Â
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
$model->setData('title', "yourValue");
$form->setValues($model->getData());
add a comment |Â
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
$model->setData('title', "yourValue");
$form->setValues($model->getData());
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
$model->setData('title', "yourValue");
$form->setValues($model->getData());
answered Sep 6 at 8:10
Ansar Husain
1,337217
1,337217
add a comment |Â
add a comment |Â
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();
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
 |Â
show 5 more comments
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();
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
 |Â
show 5 more comments
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();
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();
answered Sep 6 at 8:35
Sukumar Gorai
4,6553424
4,6553424
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
 |Â
show 5 more comments
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
 |Â
show 5 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password