Magento 2 Ui Component: how to set serializable field for dynamic rows

Magento 2 Ui Component: how to set serializable field for dynamic rows

August 10, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 Ui Component: how to set serializable field for dynamic rows

Setting Serializable Fields for Dynamic Rows in Magento 2 UI Components

In this post, we’ll dive into how to set specific components as serializable within dynamic rows of your Magento 2 UI forms, based on the scenario provided by the developer.

Understanding Serialization in Magento 2

Magento 2 uses JSON to store form data. When dealing with dynamic rows or complex nested elements, those values need to be serialized before being stored and deserialized when retrieved.

The _serializeFields method you found is responsible for serializing the fields marked as serializable:

protected function _serializeFields(\Magento\Framework\Model\AbstractModel $object)
{
    foreach ($this->_serializableFields as $field => $parameters) {
        list($serializeDefault, $unserializeDefault) = $parameters;
        $this->_serializeField($object, $field, $serializeDefault, isset($parameters[2]));
    }
}

The $_serializableFields array contains the fields that should be serialized. It’s a public property of the UI component, which means you can add your dynamic row fields to this array manually.

Setting Your Component as Serializable

To set your specific component as serializable, you’ll need to:

  1. Define an array for your dynamic rows in the ui_component XML file.
  2. Within that array, specify a serializer property with the name of the field you want to serialize.

For example, if you have a dynamic row named categories_mapping, within its container declaration, add this serializer:

<argument name="serializer" xsi:type="string">Triboo\RelatedCreator\Model\Serializer\CategoryMapping</argument>

The serializer class should extend \Magento\Framework\Serialize\SerializerInterface and handle the serialization of your data.

Adding Your Dynamic Row to $_serializableFields

To get your dynamic row added to the $_serializableFields array, you’ll need to override the UI component constructor or use a plugin.

Overriding the Constructor

In your custom UI component class, you can override the parent constructor and add your fields manually:

public function __construct(
    ...
) {
    $this->_serializableFields = [
        'category' => [true],
        'include_subcat' => [true],
        // Add other dynamic row fields here...
    ];
    parent::__construct(...);
}

Using a Plugin

You can use an after-constructor plugin to add your fields:

$pluginInfo = [
    'name' => 'Triboo_RelatedCreator',
    'after' => 'Magento\Ui\Component\Form'
];
$subject = $this->pluginManager->getSubject();
$this->pluginManager->add(
    'Triboo_RelatedCreator_Component_Form_CategoriesMapping_Plugin',
    $pluginInfo,
    [
        'before' => [$subject, '_construct'],
    ]
);

In the plugin class:

public function afterConstruct(Ui\DataProvider\AbstractDataProvider $subject)
{
    $subject->_serializableFields = [
        'category' => [true],
        // Add other dynamic row fields here...
    ];
}

Conclusion

Setting serializable fields for dynamic rows in Magento 2 UI components requires a good understanding of how the framework handles JSON serialization.

Remember to:

  • Define your serializer class extending \Magento\Framework\Serialize\SerializerInterface.
  • Add an argument for the serializer in your dynamic row container.
  • Either override the constructor or use a plugin to add your fields to $_serializableFields.

By following these steps, you can ensure that your dynamic rows are properly serialized and stored when saving the form data.

For more advanced Magento 2 UI component customizations and best practices, check out our other posts on Laravel Company.

Happy coding!

Magento 2 Configuration JavaScript

Our network