6

Hi i'm new at magento 2 and i want to disable Ui Component field while creating item, and enable the field while editing.

Well, following this tutorial: Disabling a UI component field upon condition in Magento 2

The tutorial is the opposite of what i was looking out, it only works to Disable while editing and not while add. I want to disable while add, and enable while editing. example

DataProvider:

class DataProvider extends AbstractDataProvider

{ /** * @var ResourceModel\Data\Collection */ protected $collection;

/**
 * @var DataPersistorInterface
 */
protected $dataPersistor;
/**
 * @var array
 */
protected $loadedData;
/**
 * @param string $name
 * @param string $primaryFieldName
 * @param string $requestFieldName
 * @param CollectionFactory $pageCollectionFactory
 * @param DataPersistorInterface $dataPersistor
 * @param array $meta
 * @param array $data
 */
public function __construct(
 $name,
 $primaryFieldName,
 $requestFieldName,
 CollectionFactory $pageCollectionFactory,
 DataPersistorInterface $dataPersistor,
 array $meta = [],
 array $data = []
) {
 $this->collection = $pageCollectionFactory->create();
 $this->dataPersistor = $dataPersistor;
 $this->meta = $this->prepareMeta($this->meta);
 parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
}
/**
 * Prepares Meta
 *
 * @param array $meta
 * @return array
 */
public function prepareMeta(array $meta)
{
 return $meta;
}
/**
 * Get data
 *
 * @return array
 */
public function getData()
{
 if (isset($this->loadedData)) {
 return $this->loadedData;
 }
 $items = $this->collection->getItems();
 foreach ($items as $page) {
 $this->loadedData[$page->getId()] = $page->getData();
 }
 $data = $this->dataPersistor->get('module_messages');
 if (!empty($data)) {
 $page = $this->collection->getNewEmptyItem();
 $page->setData($data);
 $this->loadedData[$page->getId()] = $page->getData();
 $this->dataPersistor->clear('module_messages');
 }
 if($_SESSION['action'] == 'edicao' AND isset($page)){
 $this->loadedData[$page->getId()]['do_we_hide_it'] = true;
 }
 return $this->loadedData;
}

}

form.xml

<field name="nome_header" formElement="input">
 <argument name="nmh" xsi:type="array">
 <item name="config" xsi:type="array">
 <item name="source" xsi:type="string">nmh</item>
 </item>
 </argument>
 <settings>
 <imports>
 <link name="disabled">${ $.provider}:data.edit</link>
 </imports>
 <validation>
 <rule name="required-entry" xsi:type="boolean">false</rule>
 </validation>
 <dataType>text</dataType>
 <label translate="true">Nome Header</label>
 <dataScope>nome_header</dataScope>
 </settings>
 </field>
asked Jun 12, 2020 at 14:12
2
  • can you provide your code for data provider, it will be easy to help you then Commented Jun 12, 2020 at 14:24
  • I put the code of DataProvider.php Commented Jun 12, 2020 at 14:29

1 Answer 1

10
  1. you can do so just by checking if your Entity id exists In your data provider.

    public function getData()
    {
     if (!empty($this->loadedData)) {
     return $this->loadedData;
     }
     $items = $this->collection->getItems();
     foreach ($items as $page) {
     $rec = $page->getData();
     $rec['edit'] = true; // true when the data is set for edit form. it can be any variable name
     $this->loadedData[$page->getId()] = $rec;
     }
     return $this->loadedData;
    }
    

And then at your field in ui-component form check for the edit variable inside the imports tag

 <field name="to_hide_feild">
 <argument name="data" xsi:type="array">
 <item name="config" xsi:type="array">
 <item name="sortOrder" xsi:type="number">10</item>
 <item name="dataType" xsi:type="string">text</item>
 <item name="label" xsi:type="string" translate="true">Title</item>
 <item name="formElement" xsi:type="string">input</item>
 <item name="dataScope" xsi:type="string">title</item>
 <item name="validation" xsi:type="array">
 <item name="required-entry" xsi:type="boolean">true</item>
 </item>
 <item name="imports" xsi:type="array">
 <item name="disabled" xsi:type="string">${ $.provider}:data.edit</item>
 </item>
 </item>
 </argument>
 </field>

if someone is using different pattern for fields in ui-component then this code will work.

 <field name="nome_header" formElement="input">
 <argument name="data" xsi:type="array">
 <item name="config" xsi:type="array">
 <item name="source" xsi:type="string">customer</item>
 </item>
 </item>
 </argument>
 <settings>
 <imports>
 <link name="disabled">${ $.provider}:data.edit</link>
 </imports>
 <validation>
 <rule name="required-entry" xsi:type="boolean">false</rule>
 </validation>
 <dataType>text</dataType>
 <disabled>true</disabled>
 <label translate="true">Nome Header</label>
 <dataScope>data</dataScope>
 </settings>
 </field>
answered Jun 12, 2020 at 14:35
9
  • It says that _loadedData is undefined.And when i add the line:$this->loadedData[$page->getId()]['do_we_hide_it'] = true; It says that $page is undefined. Commented Jun 12, 2020 at 14:45
  • sorry it was a typing mistake. I fixed it. just remove '_' from loadData variable Commented Jun 12, 2020 at 14:46
  • 1
    I got the conclusion that $page is only Defined when i edit and existing product. and its undefined when i try to create a new product. Commented Jun 12, 2020 at 14:47
  • yes. there are no collections then so the foreach loop does not run. Commented Jun 12, 2020 at 14:48
  • Ok, when i tried WITHOUT this line: $this->loadedData[$page->getId()]['do_we_hide_it'] = true; I doesn't shows any error, but don't disable any field. And When a tried WITH that line it says $page is undefined. Commented Jun 12, 2020 at 14:55

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.