0

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,

  • could you please post your code. – Dharmendra Jadav Sep 7 '18 at 9:15
  • @DharmendraJadav. Thanks for your quick response. I won't mind pasting code here but it is similar to the one which is written in the reference thread. There are many fields in admin grid form and I want to hide few based on php condition. Still, if you want then I'll paste my code of the specific file which you want to see. Let me know. Thanks. – Patrick W Sep 7 '18 at 9:20
4

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.

  • Hello @Dharmendra. You are a life saver. This solution worked like a charm. Not fully but I just wanted some hint and I got it from your code. I have just changed a little bit in getMeta() function. I have used ['visible'] = 1 or 0 instead of ['disabled']. Thanks a lot. – Patrick W Sep 7 '18 at 9:56
  • "I have used ['visible'] = 1 or 0 instead of ['disabled']", That's why i am asking about the code. – Dharmendra Jadav Sep 7 '18 at 10:00
  • Hello @Dharmendra. I think you just misunderstood me. Earlier, I have not used code which you posted recently. I just informed you that I have used visible property in the code you provided. Otherwise, your code works fine. Cheers! – Patrick W Sep 7 '18 at 10:03

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.