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– Dharmendra Jadav2018年09月07日 09:15:59 +00:00Commented 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.Pratik Navapara– Pratik Navapara2018年09月07日 09:20:14 +00:00Commented Sep 7, 2018 at 9:20
2 Answers 2
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.Pratik Navapara– Pratik Navapara2018年09月07日 09:56:42 +00:00Commented 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.Dharmendra Jadav– Dharmendra Jadav2018年09月07日 10:00:33 +00:00Commented 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!Pratik Navapara– Pratik Navapara2018年09月07日 10:03:46 +00:00Commented Sep 7, 2018 at 10:03
-
I am getting empty array when print meta in this fuctionMage2 Beginner– Mage2 Beginner2020年09月25日 09:23:34 +00:00Commented Sep 25, 2020 at 9:23
-
@DharmendraJadav can you please answer this magento.stackexchange.com/questions/328895/…amith lal– amith lal2021年01月06日 01:20:39 +00:00Commented Jan 6, 2021 at 1:20
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;
}
Explore related questions
See similar questions with these tags.