Question

I'm new in Magento 2 and I couldn't figure out what is the difference in doing a admin form module in PHP, calling a class and then using formFactory to create the form:

<referenceContainer name="content">
            <block class="BVZ\Designer\Block\Adminhtml\Post\Edit" name="designer_post_edit"/>
</referenceContainer>

[...]

$form = $this->_formFactory->create(
            [
                'data' => [
                    'id' => 'edit_form',
                    'action' => $this->getData('action'),
                    'method' => 'post',
                    'enctype' => 'multipart/form-data'
                ]
            ]

or calling a Ui component and using a xml fieldset

<body>
        <referenceContainer name="content">
            <uiComponent name="stores_index_edit"/>
        </referenceContainer>
</body>

[...]

<field name="store_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="visible" xsi:type="boolean">false</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">store</item>
                </item>
            </argument>
</field>

Is it a method better than the other or it has different proposals?

Was it helpful?

Solution

These are the difference :

FormFactory and XML :

  • Easy to debug.
  • Relatively easy to build non-standard grids.
  • You got full control of what happens.
  • You can use your knowledge from M1 to do it.
  • Grid is not that flexible or extensible.
  • you need to write the same code over and over again.
  • More code to test or that can break

Ui Components

  • Difficult to debug

  • Difficult to build non standard grids

  • It's mostly configuration. So you write less code, so less code that can break

  • you get a cool grid with show/hide columns, full text search, inline edit, export built in and maybe others.

  • It can easily be extended with just another xml file in a different module

  • any new feature magento rolls out for the grids you will get it automatically in your grid.

  • Not very much control over what happens

    I suggest you to use ui-components instead of FormFactory.

OTHER TIPS

According to DevDocs :

UI Component are designed for simple and flexible user interface (UI) rendering. Components are responsible for rendering result page fragments and providing/supporting further interactions of JavaScript components and server.

Basicly UI componet is combination of XML and JS, we configur page using XML Declaration and it uses Magneot JS class like UIElement, UIClass or UICollection. It may use phtml templates.

You can check this working example(Magento-2-custom-module-CRUD) for create model and CRUD operation using UI Component fORM and GRID

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top