Magento – Adding label in ui-component form

magento-2.1ui-formuicomponent

In UI component form, input fields(text and text area) are working allright. But when i try to replace it with a label it gives an error.

Code:

<field name="name">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string">Name</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="formElement" xsi:type="string">label</item>
            </item>
        </argument>
</field>

Error:

Exception #0 (Magento\Framework\Exception\LocalizedException): The requested component ("label") is not found. Before using, you must add the implementation.

I have researched a lot and couldn't find any worth mentioning solution.

Best Answer

Magento 2 has it's own Ui module which contains all Form elements including Checkbox, CheckboxSet, Hidden, Input, MultiSelect, Radio, Select, Textarea, Wysiwyg. but It has not a Label element.

So If you want to add a field which contains dynamic text just for visible purpose then you can use container.

uiComponent form:

<container name="note_container">
   <argument name="data" xsi:type="array">
       <item name="config" xsi:type="array">
          <item name="sortOrder" xsi:type="number">30</item>
       </item>
   </argument>
   <htmlContent name="html_content">
       <argument name="block" xsi:type="object"><VendorName>\<ModuleName>\Block\Adminhtml\<CustomClass></argument>
        </htmlContent>
 </container>

Custom Class can contains your dynamic value in html or can call custom template file:

<?php

namespace <VendorName>\<ModuleName>\Block\Adminhtml;
use Magento\Framework\View\Element\Template;

class CustomClass extends Template
{
    protected $_template = 'custom.phtml';
}

custom.phtml can contains your visible data on form.

Related Topic