Question

I have so many fields in my admin grid form which made using UI component. I want to hide some of the fields using php condition. I know that it can done using DataProvider file and I also followed this thread Disabling a UI component field upon condition in Magento 2.

But it didn't work for me. Did any one has done this before having any idea/solution apart from this?

Please guide. That would be really handy.

Thanks,

Was it helpful?

Solution

Override getMeta function in your dataprovider and set value like this.

my form.xml

    <fieldset name="fieldset_name">            
            <field name="attribute_id">
                <argument name="data" xsi:type="array">
                    <item name="options" xsi:type="object">Vendor\Module\Model\Source\Attributes</item>
                    <item name="config" xsi:type="array">
                        <item name="dataType" xsi:type="string">text</item>
                        <item name="formElement" xsi:type="string">select</item>
                        <item name="label" xsi:type="string" translate="true">Attribute</item>
                        <item name="component" xsi:type="string">Vendor_Module/js/form/element/select-option</item>                    
                        <item name="dataScope" xsi:type="string">attribute_id</item>
                        <item name="componentType" xsi:type="string">select</item>
                        <item name="validation" xsi:type="array">
                            <item name="required-entry" xsi:type="boolean">true</item>
                        </item>
                    </item>
                </argument>
            </field>
</fieldset>

My DataProvide.

public function getMeta()
    {   
        $meta = parent::getMeta();
        $id = $this->request->getParam('id');
        if(<<Your Condition>>){

            $meta['fieldset_name']['children']['attribute_id']['arguments']['data']['config']['disabled'] = 1;
        }
        return $meta;
    }

I have disable form field using this. It's work fine. you can try.

If you still any query let me know.

OTHER TIPS

I have a disabled fieldset in add new form based on entity_id

add the following code in your data provider class

public function getMeta()
    {
        $meta = parent::getMeta();
        $id = $this->request->getParam('entity_id');
        if(isset($id)){

           $meta['fieldset_name']['arguments']['data']['config']['visible'] = 1;


        }
        else{
           $meta['fieldset_name']['arguments']['data']['config']['visible'] = 0;


        }
        return $meta;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top