Magento 2 How to add country and state dropdown in admin form? [duplicate]How To Add Country And State Dropdown In Admin Side in Magento2How To Add Country And State Dropdown In Admin Side in Magento2Magento 2 - State/Province dropdown and Country Dropdown in Contact formui_component form state/region field update after country select (M 2.1)How to add new country in country dropdown in magento2.2.1how to create city state country dropdown UIComponent in magento 2 admin formMagento2 How to add country and state drop down in customer account information tab and display state based on country selected in adminHow can I add State and Country attribute for customer in Admin, Magento2Magento2 : How to show country and state dropdown field in customer account information section in admin?Magento 2: admin grid form select dropdown relationshipNot getting region dropdown options after changing the country in magento 2 admin edit form
Software for validating answers from students
Why aren't rockets built with truss structures inside their fuel & oxidizer tanks to increase structural strength?
Word for an event that will likely never happen again
Is this n-speak?
Why is Python 2.7 still the default Python version in Ubuntu?
What kind of liquid can be seen 'leaking' from the upper surface of the wing of a Boeing 737-800?
Simplification of numbers
The cat exchanges places with a drawing of the cat
A torrent of foreign terms
How to take the beginning and end parts of a list with simpler syntax?
Are there really no countries that protect Freedom of Speech as the United States does?
What is the difference between 王 and 皇?
Website error: "Walmart can’t use this browser"
Why is statically linking glibc discouraged?
Is there a way to count the number of lines of text in a file including non-delimited ones?
Modeling the uncertainty of the input parameters
Can the IPA represent all languages' tones?
How to remove ambiguity: "... lives in the city of H, the capital of the province of NS, WHERE the unemployment rate is ..."?
Help, I cannot decide when to start the story
Is it possible to grow new organs through exposure to radioactivity?
Do beef farmed pastures net remove carbon emissions?
Update Office without opening an Office application
How far did Gandalf and the Balrog drop from the bridge in Moria?
My cat is a houdini
Magento 2 How to add country and state dropdown in admin form? [duplicate]
How To Add Country And State Dropdown In Admin Side in Magento2How To Add Country And State Dropdown In Admin Side in Magento2Magento 2 - State/Province dropdown and Country Dropdown in Contact formui_component form state/region field update after country select (M 2.1)How to add new country in country dropdown in magento2.2.1how to create city state country dropdown UIComponent in magento 2 admin formMagento2 How to add country and state drop down in customer account information tab and display state based on country selected in adminHow can I add State and Country attribute for customer in Admin, Magento2Magento2 : How to show country and state dropdown field in customer account information section in admin?Magento 2: admin grid form select dropdown relationshipNot getting region dropdown options after changing the country in magento 2 admin edit form
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
This question already has an answer here:
How To Add Country And State Dropdown In Admin Side in Magento2
3 answers
I need to add Country and State Dropdown in Magento 2 admin form.
Please help me.
magento2 adminform country-regions dropdown-options
marked as duplicate by Amit Bera♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Aug 2 at 5:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How To Add Country And State Dropdown In Admin Side in Magento2
3 answers
I need to add Country and State Dropdown in Magento 2 admin form.
Please help me.
magento2 adminform country-regions dropdown-options
marked as duplicate by Amit Bera♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Aug 2 at 5:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
please go thorough this "magento.stackexchange.com/questions/89856/…"
– Rv Singh
Aug 2 at 5:13
add a comment |
This question already has an answer here:
How To Add Country And State Dropdown In Admin Side in Magento2
3 answers
I need to add Country and State Dropdown in Magento 2 admin form.
Please help me.
magento2 adminform country-regions dropdown-options
This question already has an answer here:
How To Add Country And State Dropdown In Admin Side in Magento2
3 answers
I need to add Country and State Dropdown in Magento 2 admin form.
Please help me.
This question already has an answer here:
How To Add Country And State Dropdown In Admin Side in Magento2
3 answers
magento2 adminform country-regions dropdown-options
magento2 adminform country-regions dropdown-options
asked Aug 2 at 5:10
Himani TayalHimani Tayal
26714 bronze badges
26714 bronze badges
marked as duplicate by Amit Bera♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Aug 2 at 5:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Amit Bera♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Aug 2 at 5:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Amit Bera♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Aug 2 at 5:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
please go thorough this "magento.stackexchange.com/questions/89856/…"
– Rv Singh
Aug 2 at 5:13
add a comment |
please go thorough this "magento.stackexchange.com/questions/89856/…"
– Rv Singh
Aug 2 at 5:13
please go thorough this "magento.stackexchange.com/questions/89856/…"
– Rv Singh
Aug 2 at 5:13
please go thorough this "magento.stackexchange.com/questions/89856/…"
– Rv Singh
Aug 2 at 5:13
add a comment |
2 Answers
2
active
oldest
votes
To add Country and State Dropdown in admin form, follow below steps:
In your form Block file:
Define variables:
protected $_regionFactory;
protected $_countryFactory;
In Construction:
public function __construct(
...
MagentoDirectoryModelRegionFactory $regionFactory,
MagentoDirectoryModelConfigSourceCountry $countryFactory,
...
)
...
$this->_regionFactory = $regionFactory;
$this->_countryFactory = $countryFactory;
...
In
initForm
:
$countries = $this->_countryFactory->toOptionArray();
$countryData = $fieldset->addField(
'mycountry',
'select',
[
'name' => 'mycountry',
'data-form-part' => $this->getData('target_form'),
'title' => __('Country'),
'label' => __('Country'),
'value' => $partner['mycountry'],
'values' => $countries
]
);
$regionCollection = $this->_regionFactory->create()->getCollection()->addCountryFilter($partner['mycountry']);
$regions = $regionCollection->toOptionArray();
$fieldset->addField(
'mystate',
'select',
[
'name' => 'mystate',
'data-form-part' => $this->getData('target_form'),
'title' => __('State'),
'label' => __('State'),
'values' => $regions,
'value' => $partner['mystate'],
]
);
$countryData->setAfterElementHtml("
<script type="text/javascript">
require([
'jquery',
'mage/template',
'jquery/ui',
'mage/translate'
],
function($, mageTemplate)
$('#edit_form').on('change', '#country_id', function(event)
$.ajax(
url : '". $this->getUrl('test/*/regionlist') . "country/' + $('#country_id').val(),
type: 'get',
dataType: 'json',
showLoader:true,
success: function(data)
$('#region_id').empty();
$('#region_id').append(data.htmlconent);
);
)
);
</script>"
);
Here $partner
is the variable which contain all form data.
add a comment |
Can you use below code
<?php
/**
* @var MagentoDirectoryModelConfigSourceCountry
*/
protected $_country;
/**
* @var MagentoDirectoryModelRegionFactory
*/
protected $_regionFactory;
// ... add them in __construct
// ...
// Add next changes in the _prepareForm method
$countries = $this->_country->toOptionArray(false, 'US');
$regionCollection = $this->_regionFactory->create()->getCollection()->addCountryFilter(
$formData['country_id']
);
$regions = $regionCollection->toOptionArray();
$fieldset->addField(
'country_id',
'select',
['name' => 'country_id', 'label' => __('Country'), 'required' => true, 'values' => $countries]
);
$fieldset->addField(
'region_id',
'select',
['name' => 'region_id', 'label' => __('State'), 'values' => $regions]
);
$this->setChild(
'form_after',
$this->getLayout()->createBlock('MagentoFrameworkViewElementTemplate')->setTemplate('Vendor_Module::js.phtml')
);
?>
> Vendor_Module::js.phtml
<script>
require([
"jquery",
"mage/adminhtml/form"
], function ($)
var updater = new RegionUpdater($('[name=country_id]')[0], $('[name=region]')[0], $('[name=region_id]')[0],
<?php echo $this->helper('MagentoDirectoryHelperData')->getRegionJson() ?>,
'hide'
);
updater.disableRegionValidation();
window.updater = updater;
);
</script>
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
To add Country and State Dropdown in admin form, follow below steps:
In your form Block file:
Define variables:
protected $_regionFactory;
protected $_countryFactory;
In Construction:
public function __construct(
...
MagentoDirectoryModelRegionFactory $regionFactory,
MagentoDirectoryModelConfigSourceCountry $countryFactory,
...
)
...
$this->_regionFactory = $regionFactory;
$this->_countryFactory = $countryFactory;
...
In
initForm
:
$countries = $this->_countryFactory->toOptionArray();
$countryData = $fieldset->addField(
'mycountry',
'select',
[
'name' => 'mycountry',
'data-form-part' => $this->getData('target_form'),
'title' => __('Country'),
'label' => __('Country'),
'value' => $partner['mycountry'],
'values' => $countries
]
);
$regionCollection = $this->_regionFactory->create()->getCollection()->addCountryFilter($partner['mycountry']);
$regions = $regionCollection->toOptionArray();
$fieldset->addField(
'mystate',
'select',
[
'name' => 'mystate',
'data-form-part' => $this->getData('target_form'),
'title' => __('State'),
'label' => __('State'),
'values' => $regions,
'value' => $partner['mystate'],
]
);
$countryData->setAfterElementHtml("
<script type="text/javascript">
require([
'jquery',
'mage/template',
'jquery/ui',
'mage/translate'
],
function($, mageTemplate)
$('#edit_form').on('change', '#country_id', function(event)
$.ajax(
url : '". $this->getUrl('test/*/regionlist') . "country/' + $('#country_id').val(),
type: 'get',
dataType: 'json',
showLoader:true,
success: function(data)
$('#region_id').empty();
$('#region_id').append(data.htmlconent);
);
)
);
</script>"
);
Here $partner
is the variable which contain all form data.
add a comment |
To add Country and State Dropdown in admin form, follow below steps:
In your form Block file:
Define variables:
protected $_regionFactory;
protected $_countryFactory;
In Construction:
public function __construct(
...
MagentoDirectoryModelRegionFactory $regionFactory,
MagentoDirectoryModelConfigSourceCountry $countryFactory,
...
)
...
$this->_regionFactory = $regionFactory;
$this->_countryFactory = $countryFactory;
...
In
initForm
:
$countries = $this->_countryFactory->toOptionArray();
$countryData = $fieldset->addField(
'mycountry',
'select',
[
'name' => 'mycountry',
'data-form-part' => $this->getData('target_form'),
'title' => __('Country'),
'label' => __('Country'),
'value' => $partner['mycountry'],
'values' => $countries
]
);
$regionCollection = $this->_regionFactory->create()->getCollection()->addCountryFilter($partner['mycountry']);
$regions = $regionCollection->toOptionArray();
$fieldset->addField(
'mystate',
'select',
[
'name' => 'mystate',
'data-form-part' => $this->getData('target_form'),
'title' => __('State'),
'label' => __('State'),
'values' => $regions,
'value' => $partner['mystate'],
]
);
$countryData->setAfterElementHtml("
<script type="text/javascript">
require([
'jquery',
'mage/template',
'jquery/ui',
'mage/translate'
],
function($, mageTemplate)
$('#edit_form').on('change', '#country_id', function(event)
$.ajax(
url : '". $this->getUrl('test/*/regionlist') . "country/' + $('#country_id').val(),
type: 'get',
dataType: 'json',
showLoader:true,
success: function(data)
$('#region_id').empty();
$('#region_id').append(data.htmlconent);
);
)
);
</script>"
);
Here $partner
is the variable which contain all form data.
add a comment |
To add Country and State Dropdown in admin form, follow below steps:
In your form Block file:
Define variables:
protected $_regionFactory;
protected $_countryFactory;
In Construction:
public function __construct(
...
MagentoDirectoryModelRegionFactory $regionFactory,
MagentoDirectoryModelConfigSourceCountry $countryFactory,
...
)
...
$this->_regionFactory = $regionFactory;
$this->_countryFactory = $countryFactory;
...
In
initForm
:
$countries = $this->_countryFactory->toOptionArray();
$countryData = $fieldset->addField(
'mycountry',
'select',
[
'name' => 'mycountry',
'data-form-part' => $this->getData('target_form'),
'title' => __('Country'),
'label' => __('Country'),
'value' => $partner['mycountry'],
'values' => $countries
]
);
$regionCollection = $this->_regionFactory->create()->getCollection()->addCountryFilter($partner['mycountry']);
$regions = $regionCollection->toOptionArray();
$fieldset->addField(
'mystate',
'select',
[
'name' => 'mystate',
'data-form-part' => $this->getData('target_form'),
'title' => __('State'),
'label' => __('State'),
'values' => $regions,
'value' => $partner['mystate'],
]
);
$countryData->setAfterElementHtml("
<script type="text/javascript">
require([
'jquery',
'mage/template',
'jquery/ui',
'mage/translate'
],
function($, mageTemplate)
$('#edit_form').on('change', '#country_id', function(event)
$.ajax(
url : '". $this->getUrl('test/*/regionlist') . "country/' + $('#country_id').val(),
type: 'get',
dataType: 'json',
showLoader:true,
success: function(data)
$('#region_id').empty();
$('#region_id').append(data.htmlconent);
);
)
);
</script>"
);
Here $partner
is the variable which contain all form data.
To add Country and State Dropdown in admin form, follow below steps:
In your form Block file:
Define variables:
protected $_regionFactory;
protected $_countryFactory;
In Construction:
public function __construct(
...
MagentoDirectoryModelRegionFactory $regionFactory,
MagentoDirectoryModelConfigSourceCountry $countryFactory,
...
)
...
$this->_regionFactory = $regionFactory;
$this->_countryFactory = $countryFactory;
...
In
initForm
:
$countries = $this->_countryFactory->toOptionArray();
$countryData = $fieldset->addField(
'mycountry',
'select',
[
'name' => 'mycountry',
'data-form-part' => $this->getData('target_form'),
'title' => __('Country'),
'label' => __('Country'),
'value' => $partner['mycountry'],
'values' => $countries
]
);
$regionCollection = $this->_regionFactory->create()->getCollection()->addCountryFilter($partner['mycountry']);
$regions = $regionCollection->toOptionArray();
$fieldset->addField(
'mystate',
'select',
[
'name' => 'mystate',
'data-form-part' => $this->getData('target_form'),
'title' => __('State'),
'label' => __('State'),
'values' => $regions,
'value' => $partner['mystate'],
]
);
$countryData->setAfterElementHtml("
<script type="text/javascript">
require([
'jquery',
'mage/template',
'jquery/ui',
'mage/translate'
],
function($, mageTemplate)
$('#edit_form').on('change', '#country_id', function(event)
$.ajax(
url : '". $this->getUrl('test/*/regionlist') . "country/' + $('#country_id').val(),
type: 'get',
dataType: 'json',
showLoader:true,
success: function(data)
$('#region_id').empty();
$('#region_id').append(data.htmlconent);
);
)
);
</script>"
);
Here $partner
is the variable which contain all form data.
answered Aug 2 at 5:14
Himani TayalHimani Tayal
26714 bronze badges
26714 bronze badges
add a comment |
add a comment |
Can you use below code
<?php
/**
* @var MagentoDirectoryModelConfigSourceCountry
*/
protected $_country;
/**
* @var MagentoDirectoryModelRegionFactory
*/
protected $_regionFactory;
// ... add them in __construct
// ...
// Add next changes in the _prepareForm method
$countries = $this->_country->toOptionArray(false, 'US');
$regionCollection = $this->_regionFactory->create()->getCollection()->addCountryFilter(
$formData['country_id']
);
$regions = $regionCollection->toOptionArray();
$fieldset->addField(
'country_id',
'select',
['name' => 'country_id', 'label' => __('Country'), 'required' => true, 'values' => $countries]
);
$fieldset->addField(
'region_id',
'select',
['name' => 'region_id', 'label' => __('State'), 'values' => $regions]
);
$this->setChild(
'form_after',
$this->getLayout()->createBlock('MagentoFrameworkViewElementTemplate')->setTemplate('Vendor_Module::js.phtml')
);
?>
> Vendor_Module::js.phtml
<script>
require([
"jquery",
"mage/adminhtml/form"
], function ($)
var updater = new RegionUpdater($('[name=country_id]')[0], $('[name=region]')[0], $('[name=region_id]')[0],
<?php echo $this->helper('MagentoDirectoryHelperData')->getRegionJson() ?>,
'hide'
);
updater.disableRegionValidation();
window.updater = updater;
);
</script>
add a comment |
Can you use below code
<?php
/**
* @var MagentoDirectoryModelConfigSourceCountry
*/
protected $_country;
/**
* @var MagentoDirectoryModelRegionFactory
*/
protected $_regionFactory;
// ... add them in __construct
// ...
// Add next changes in the _prepareForm method
$countries = $this->_country->toOptionArray(false, 'US');
$regionCollection = $this->_regionFactory->create()->getCollection()->addCountryFilter(
$formData['country_id']
);
$regions = $regionCollection->toOptionArray();
$fieldset->addField(
'country_id',
'select',
['name' => 'country_id', 'label' => __('Country'), 'required' => true, 'values' => $countries]
);
$fieldset->addField(
'region_id',
'select',
['name' => 'region_id', 'label' => __('State'), 'values' => $regions]
);
$this->setChild(
'form_after',
$this->getLayout()->createBlock('MagentoFrameworkViewElementTemplate')->setTemplate('Vendor_Module::js.phtml')
);
?>
> Vendor_Module::js.phtml
<script>
require([
"jquery",
"mage/adminhtml/form"
], function ($)
var updater = new RegionUpdater($('[name=country_id]')[0], $('[name=region]')[0], $('[name=region_id]')[0],
<?php echo $this->helper('MagentoDirectoryHelperData')->getRegionJson() ?>,
'hide'
);
updater.disableRegionValidation();
window.updater = updater;
);
</script>
add a comment |
Can you use below code
<?php
/**
* @var MagentoDirectoryModelConfigSourceCountry
*/
protected $_country;
/**
* @var MagentoDirectoryModelRegionFactory
*/
protected $_regionFactory;
// ... add them in __construct
// ...
// Add next changes in the _prepareForm method
$countries = $this->_country->toOptionArray(false, 'US');
$regionCollection = $this->_regionFactory->create()->getCollection()->addCountryFilter(
$formData['country_id']
);
$regions = $regionCollection->toOptionArray();
$fieldset->addField(
'country_id',
'select',
['name' => 'country_id', 'label' => __('Country'), 'required' => true, 'values' => $countries]
);
$fieldset->addField(
'region_id',
'select',
['name' => 'region_id', 'label' => __('State'), 'values' => $regions]
);
$this->setChild(
'form_after',
$this->getLayout()->createBlock('MagentoFrameworkViewElementTemplate')->setTemplate('Vendor_Module::js.phtml')
);
?>
> Vendor_Module::js.phtml
<script>
require([
"jquery",
"mage/adminhtml/form"
], function ($)
var updater = new RegionUpdater($('[name=country_id]')[0], $('[name=region]')[0], $('[name=region_id]')[0],
<?php echo $this->helper('MagentoDirectoryHelperData')->getRegionJson() ?>,
'hide'
);
updater.disableRegionValidation();
window.updater = updater;
);
</script>
Can you use below code
<?php
/**
* @var MagentoDirectoryModelConfigSourceCountry
*/
protected $_country;
/**
* @var MagentoDirectoryModelRegionFactory
*/
protected $_regionFactory;
// ... add them in __construct
// ...
// Add next changes in the _prepareForm method
$countries = $this->_country->toOptionArray(false, 'US');
$regionCollection = $this->_regionFactory->create()->getCollection()->addCountryFilter(
$formData['country_id']
);
$regions = $regionCollection->toOptionArray();
$fieldset->addField(
'country_id',
'select',
['name' => 'country_id', 'label' => __('Country'), 'required' => true, 'values' => $countries]
);
$fieldset->addField(
'region_id',
'select',
['name' => 'region_id', 'label' => __('State'), 'values' => $regions]
);
$this->setChild(
'form_after',
$this->getLayout()->createBlock('MagentoFrameworkViewElementTemplate')->setTemplate('Vendor_Module::js.phtml')
);
?>
> Vendor_Module::js.phtml
<script>
require([
"jquery",
"mage/adminhtml/form"
], function ($)
var updater = new RegionUpdater($('[name=country_id]')[0], $('[name=region]')[0], $('[name=region_id]')[0],
<?php echo $this->helper('MagentoDirectoryHelperData')->getRegionJson() ?>,
'hide'
);
updater.disableRegionValidation();
window.updater = updater;
);
</script>
answered Aug 2 at 5:17
Soundararajan mSoundararajan m
569 bronze badges
569 bronze badges
add a comment |
add a comment |
please go thorough this "magento.stackexchange.com/questions/89856/…"
– Rv Singh
Aug 2 at 5:13