3

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,

asked Sep 7, 2018 at 9:14
2
  • could you please post your code. Commented Sep 7, 2018 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. Commented Sep 7, 2018 at 9:20

2 Answers 2

10

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.

answered Sep 7, 2018 at 9:31
6
  • 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. Commented Sep 7, 2018 at 9:56
  • "I have used ['visible'] = 1 or 0 instead of ['disabled']", That's why i am asking about the code. Commented Sep 7, 2018 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! Commented Sep 7, 2018 at 10:03
  • I am getting empty array when print meta in this fuction Commented Sep 25, 2020 at 9:23
  • @DharmendraJadav can you please answer this magento.stackexchange.com/questions/328895/… Commented Jan 6, 2021 at 1:20
2

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;
 }
answered Sep 7, 2020 at 5:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.