Skip to main content

magento Magento 2 Field dependency in ui form

edited body
Source Link

I have created a ui component form. I want to display some fields based on status value. Display other fields only if status is 1. How Can I acheiveachieve it?

I have created a ui component form. I want to display some fields based on status value. Display other fields only if status is 1. How Can I acheive it?

I have created a ui component form. I want to display some fields based on status value. Display other fields only if status is 1. How Can I achieve it?

Tweeted twitter.com/StackMagento/status/818877251148193793
Source Link
Jancy Abraham
  • 2.7k
  • 8
  • 47
  • 73

magento 2 Field dependency in ui form

I have created a ui component form. I want to display some fields based on status value. Display other fields only if status is 1. How Can I acheive it?

Here is my code

<field name="status">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Status</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="dataType" xsi:type="string">number</item>
                <item name="formElement" xsi:type="string">select</item>
                <item name="source" xsi:type="string">status</item>
                <item name="dataScope" xsi:type="string">status</item>
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">true</item>
                </item>
                <item name="options" xsi:type="array">                       
                        <item name="0" xsi:type="array">
                            <item name="label" xsi:type="string">Pending</item>
                            <item name="value" xsi:type="string">0</item>
                        </item>
                        <item name="1" xsi:type="array">
                            <item name="label" xsi:type="string">Allowed</item>
                            <item name="value" xsi:type="string">1</item>
                        </item>
                        <item name="2" xsi:type="array">
                            <item name="label" xsi:type="string">Not Allowed</item>
                            <item name="value" xsi:type="string">2</item>
                        </item>
                </item>
            </item>
        </argument>
    </field>
    <field name="title">
         <argument name="data" xsi:type="array">
             <item name="config" xsi:type="array">
                 <item name="label" xsi:type="string">Title</item>
                 <item name="visible" xsi:type="boolean">true</item>
                 <item name="dataType" xsi:type="string">text</item>
                 <item name="formElement" xsi:type="string">textarea</item>
                 <item name="source" xsi:type="string">title</item>
                 <item name="required" xsi:type="boolean">true</item>
                 <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                 </item>                
             </item>
         </argument>
    </field>  
    <field name="category_ids">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Magento\Catalog\Ui\Component\Product\Form\Categories\Options</item>
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Category</item>
                    <item name="componentType" xsi:type="string">field</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="component" xsi:type="string">Magento_Catalog/js/components/new-category</item>
                    <item name="elementTmpl" xsi:type="string">ui/grid/filters/elements/ui-select</item>
                    <item name="filterOptions" xsi:type="boolean">true</item>
                    <item name="showCheckbox" xsi:type="boolean">true</item>
                    <item name="disableLabel" xsi:type="boolean">true</item>
                    <item name="multiple" xsi:type="boolean">true</item>
                    <item name="levelsVisibility" xsi:type="number">1</item>
                    <item name="required" xsi:type="boolean">false</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                    <item name="listens" xsi:type="array">
                        <item name="${ $.namespace }.${ $.namespace }:responseData" xsi:type="string">setParsed</item>
                    </item>
                </item>
            </argument>
        </field>
        <field name="custom_field">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Custom Field</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">number</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="source" xsi:type="string">custom_field</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                    <item name="options" xsi:type="array">                       
                        <item name="0" xsi:type="array">
                            <item name="label" xsi:type="string">No</item>
                            <item name="value" xsi:type="string">0</item>
                        </item>
                        <item name="1" xsi:type="array">
                            <item name="label" xsi:type="string">Yes</item>
                            <item name="value" xsi:type="string">1</item>
                        </item>
                    </item>
                </item>
            </argument>
        </field>