0

I am using Magento 2.1.0, and i created one custom module. i created form through ui_component. now i want to create one city dropdown and one status is_active field.

How is this both possible with Ui_component?

For Status:

<field name="is_active">
    <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Magento\Cms\Model\Page\Source\IsActive</item>
                <item name="config" xsi:type="array">
       <item name="formElement" xsi:type="string">input</item>
                <item name="filter" xsi:type="string">select</item>
                <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
                <item name="editor" xsi:type="string">select</item>
                <item name="dataType" xsi:type="string">select</item>
    <item name="validation" xsi:type="array"><item name="required-entry" xsi:type="string">false</item>
    </item>
                <item name="label" xsi:type="string" translate="true">Status</item>
            </item>
    </argument>
    </field>

For City Dropdown:

field name="city">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
                <item name="filter" xsi:type="string">select</item>
    <item name="formElement" xsi:type="string">select</item>
                <item name="editor" xsi:type="string">select</item>
                <item name="visible" xsi:type="boolean">false</item>
                <item name="dataType" xsi:type="string">select</item>
                <item name="label" xsi:type="string" translate="true">city</item>
                <item name="sortOrder" xsi:type="number">50</item>
            </item>
        </argument>
    </field>
2
  • please add your code of ui_component, which you have tried. – Rakesh Jesadiya Jan 25 '17 at 4:51
  • Sorry, now check – sam Jan 25 '17 at 5:05
2

Edit

You need to add

<item name="options" xsi:type="object">Your\Module\Model\SomeFile</item>

So your field code

<field name="city">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Your\Module\Model\SomeFile</item>
        <item name="config" xsi:type="array">
            <item name="dataType" xsi:type="string">text</item>
            <item name="label" xsi:type="string" translate="true">City</item>
            <item name="formElement" xsi:type="string">select</item>
            <item name="source" xsi:type="string">page</item>
            <item name="dataScope" xsi:type="string">city</item>
            <item name="caption" xsi:type="string" translate="true">-- Please Select --</item>
        </item>
    </argument>
</field>

And your module should like this

<?php
namespace Your\Module\Model;

use Magento\Framework\Data\OptionSourceInterface;

/**
 * Class IsActive
 */
class SomeFile implements OptionSourceInterface
{

    /**
     * Get options
     *
     * @return array
     */
    public function toOptionArray()
    {

            $options = [
                [
                'label' => 'Test',
                'value' => '1',
                ],
                [
                'label' => 'Test2',
                'value' => '2',
                ]
            ];
        return $options;
    }
}
9
  • Thank You for Reply. I tried like this, but it is showing error like xxx\xxx\Model\City\CityOption.php does not exist – sam Jan 25 '17 at 5:50
  • can you clear var/generation folder and flush cache and try – MeenakshiSundaram R Jan 25 '17 at 5:51
  • I did that, then also showing same error – sam Jan 25 '17 at 5:56
  • can you show your CityOption.php file – MeenakshiSundaram R Jan 25 '17 at 5:57
  • 1
    @sam, the code is correct, just use <item name="options" xsi:type="object">Your\Module\Model\SomeFile</item> instead of <item name="options" xsi:type="object">Your\Module\Model\SomeFile.php</item> – Jaimin Sutariya Jan 25 '17 at 6:15

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.