0

First i have created country drop-down in back-end system configuration, using source model as

<source_model>Magento\Directory\Model\Config\Source\Country</source_model>

Next based on country selected, I need to populate states of that country in a multi-select field below country field.

Currently, there is option for loading state a single select drop-down. But I need it as multi-select attribute..

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="new_section">
            <group id="general">
                 <field id="address_country_id" translate="label comment" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Applicable Countries</label>
                    <source_model>Magento\Directory\Model\Config\Source\Country</source_model>
                    <frontend_class>countries</frontend_class>
                </field>
                <field id="address_region_id" translate="label comment" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Applicable Region/State</label>
                </field>
            </group>
        </section>
    </system>
</config>

Here is the screenshot

enter image description here

  • Can you update your question what you have tried for single select dropdown? – Sukumar Gorai Jun 14 at 13:40
  • I have updated my question – aravind Jun 14 at 14:08
0

Please try below to get the multiselect dropdown

Ship to Specific Countries Magento\Directory\Model\Config\Source\Country 1

Thanks

0

1.If you need multi-select field at the system.xml of all country list then add the filed just like below in your group.

  <field id="address_country_id" translate="label comment" type="multiselect" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Applicable Countries</label>
            <source_model>Magento\Directory\Model\Config\Source\Country</source_model>
            <frontend_class>countries</frontend_class>
   </field>

2.please flush the Cache.

  • There are two fields country and state. I want the country as select field and based on country selected respected states for that country as multiselect. – aravind Jun 17 at 4:40
0

you need to use jquery and ajax, fetch the id of the selected option in jquery and call a callback ajax function which redirects to its controller and update the second dependent dropdown using its ID in Success part

0

Add new field in UI form

<field name="group">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">{Vendor}\{Module}\Model\Model\Source\Myvalues</item>
        <item name="config" xsi:type="array">
            <item name="dataType" xsi:type="string">text</item>
            <item name="label" translate="true" xsi:type="string">Your Label</item>
            <item name="formElement" xsi:type="string">multiselect</item>
        </item>
    </argument>
</field>

Now create Myvalues.php to provide multiselect options at

app/code/{Vendor}/{Module}/Model/Source/Myvalues.php

<?php

namespace Vendor\Module\Model\Source\Myvalues;

class Myvalues implements \Magento\Framework\Option\ArrayInterface
{
    public function toOptionArray()
    {
        return [
            ['value' => 1, 'label' => __('Test One')],
            ['value' => 2, 'label' => __('Test Two')],
            ['value' => 3, 'label' => __('Test Three')],
        ];
    }
}

Your Answer

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

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