Pregunta

I have a UI component defining some fields. How do I disable the field upon a certain condition? In other words, use <item name="disabled" xsi:type="boolean">true</item> only when a condition is met. Here's my field.

<field name="name">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="dataType" xsi:type="string">text</item>
                <item name="label" xsi:type="string" translate="true">Profile Name</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">profile</item>
                <item name="dataScope" xsi:type="string">name</item>
            </item>
        </argument>
    </field>
¿Fue útil?

Solución

First set:

<item name="disabled" xsi:type="string">${ $.provider }:data.do_we_hide_it</item>

Suppose Vendor\Extension\Model\Notification\DataProvider is your data provider for UI:

<dataSource name="notification_edit_data_source">
    <argument name="dataProvider" xsi:type="configurableObject">
        <argument name="class" xsi:type="string">Vendor\Extension\Model\Notification\DataProvider</argument>
        <argument name="name" xsi:type="string">notification_edit_data_source</argument>

Then in its getData function add the following lines:

public function getData(){
.....
.....
if(condition1)
    $this->loadedData[$entity_id]['do_we_hide_it'] = true;
else
    $this->loadedData[$entity_id]['do_we_hide_it'] = false;

See the core files vendor/magento/module-catalog/view/adminhtml/ui_component/category_form.xml line 377 and vendor/magento/module-catalog/Model/Category/DataProvider.php line 303 for an example.

Otros consejos

I tried Konstantin's solution but for some reason, it didn't work for me.

I managed to make it work by using the following syntax:

<field name="name">
    <argument>
      ...
    </argument>
    <settings>
        <imports>
            <link name="disabled">${ $.provider}:data.do_we_hide_it</link>
        </imports>
    </settings>
</field>

Hope it helps.

Edit :

The argument node is not required but if it exists, settings node must be placed after. It is the last one in the xsd definition in M2.3. The xml comes in addition with the accepted answer.

You can override getMeta() function in your DataProvider class

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

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


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


        }
        return $meta;
    }

This worked for me.

So for all of you struggling with this, here is another "solution". I combined the answers above to the one that works for me:

My item url looks like this: specialshipment/item/edit/id/172/key... this important to understand because you need to retrieve the id and not the entity_id as was my first mistake.

Because I needed to hide my dropdown in case of an existing shipment I added a class to my field. To do this you need the so called "additionalClasses" attribute. It will be "hidden" if it is an existing shipment and "use_dropdown" when the shipment is new (since we would like to use the dropdown).

the answer from @smitaKagwade was what helped a lot! Here is my ui form with the special class:

<fieldset name="general">
....
    <field name="shipment_container_size_id">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Vendor\SpecialShipments\Model\Config\Source\ShipContainerSize</item>
                <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">shipment_container_size_id</item>
                    <item name="label" xsi:type="string" translate="true">Ship Container Size</item>
                    <item name="dataType" xsi:type="string">select</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="additionalClasses" xsi:type="string">use_dropdown</item>
                </item>
            </argument>
        </field>

In the above note the line saying item name="additionalClasses" ...use_dropdown

So my dropdown (ShipContainerSize) field in the form gets a css class called use_dropdown.

I want to hide it if the shipment already exists and the container size is already chosen. For this I need to go to my DataProvider and in my construct function I make sure to get the Magento\Framework\App\RequestInterface $request. Now I can apply smitaKagwade's solution making use of the getParam('id') for the url id as mentioned at the beginning.

As Smita Kagwade mentioned, I need my field set name (as seen above it is "general"), my field name ("shipment_container_size_id") and the config attribute I want to change: additionalClasses. So here is the code:

public function getMeta()
    {
        $meta = parent::getMeta();
        $itemId = $this->request->getParam('id');
        if (isset($itemId) && $itemId > 0) {
            $meta['general']['children']['shipment_container_size_id']['arguments']['data']['config']['additionalClasses'] = 'hidden';
        }
        else{
            $meta['general']['children']['shipment_container_size_id']['arguments']['data']['config']['additionalClasses'] = 'use_dropdown';
        }
        return $meta;
    }

And it works! It hides the drowpdown on existing shipment items, and shows it on new ones. I hope this elaborate solution will help others :-)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top