Magento

Magento: ui_component form state/region field update after country.

Pinterest LinkedIn Tumblr

you can add this code in your .xml file

<fieldset name="address">
...
<field name="country_id">
        <argument name="data" xsi:type="array">
            <item name="options" xsi:type="object">Magento\Directory\Model\ResourceModel\Country\Collection</item>
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Country</item>
                <item name="formElement" xsi:type="string">select</item>
                <item name="sortOrder" xsi:type="number">100</item>
                <item name="source" xsi:type="string">address</item><!-- fieldset name -->
                <item name="dataScope" xsi:type="string">country</item><!-- 'country' - db table column name where country code is saved --> 
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">true</item>
                </item>
            </item>
        </argument>
    </field>
    <field name="region_id">
        <argument name="data" xsi:type="array">
            <item name="options" xsi:type="object">Magento\Directory\Model\ResourceModel\Region\Collection</item>
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">State/Province</item>
                <item name="formElement" xsi:type="string">select</item>
                <item name="sortOrder" xsi:type="number">110</item>
                <item name="source" xsi:type="string">address</item><!-- fieldset name -->
                <item name="customEntry" xsi:type="string">state_province</item><!-- 'state_province' - db table column name where region name is saved -->
                <item name="dataScope" xsi:type="string">state_province_id</item><!-- 'state_province_id' - db table column name where region id is saved -->
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">true</item>
                </item>
                <item name="filterBy" xsi:type="array">
                    <item name="target" xsi:type="string">${ $.provider }:${ $.parentScope }.country</item><!-- 'country' - db table column name where country code is saved -->
                    <item name="field" xsi:type="string">country_id</item><!-- country_id field name -->
                </item>
            </item>
        </argument>
    </field>
    <field name="region">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="dataType" xsi:type="string">text</item>
                <item name="label" xsi:type="string" translate="true">State/Province</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="sortOrder" xsi:type="number">115</item>
                <item name="source" xsi:type="string">address</item><!-- fieldset name -->
                <item name="customEntry" xsi:type="string">state_province</item><!-- 'state_province' - db table column name where region name is saved -->
                <item name="dataScope" xsi:type="string">state_province</item><!-- 'state_province' - db table column name where region name is saved -->
                <item name="visible" xsi:type="boolean">false</item>
            </item>
        </argument>
    </field>
...
</fieldset>
Faster PHP Cloud Hosting