I’m trying to create a simple Magento 2 admin form with the following requirements, but I’m running into issues. Here’s what I want:
Form Fields:
- A hidden form_id field with a value of 1.
- A visible title field with an initial value of “Default Value”. This should be retrieved from the data provider
- Data Provider
- UI Component XML
- The form_id field should be invisible, and the title field should be visible.
- The form is standalone and does not rely on a grid or collection.
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="provider" xsi:type="string">mobile_settings_form.form_form_data_source</item>
</item>
<item name="label" xsi:type="string" translate="true">General Information</item>
<item name="template" xsi:type="string">templates/form/collapsible</item>
</argument>
<settings>
<namespace>mobile_settings_form</namespace>
<dataScope>data</dataScope>
<deps>
<dep>mobile_settings_form.form_form_data_source</dep>
</deps>
</settings>
<dataSource name="form_form_data_source">
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
</item>
</argument>
<settings>
<submitUrl path="cms/form/save"/>
</settings>
<dataProvider class="SwiftsolutionsMobileAdminPanelModelFormDataProvider"
name="form_form_data_source">
<settings>
<requestFieldName>form_id</requestFieldName>
<primaryFieldName>form_id</primaryFieldName>
</settings>
</dataProvider>
</dataSource>
<fieldset name="general">
<settings>
<label/>
</settings>
<!-- Invisible form_id field -->
<field name="form_id" formElement="input">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">form</item>
</item>
</argument>
<settings>
<dataType>text</dataType>
<visible>false</visible> <!-- This field is invisible -->
<dataScope>form_id</dataScope>
</settings>
</field>
<!-- Visible title field -->
<field name="title" sortOrder="20" formElement="input">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">form</item>
</item>
</argument>
<settings>
<validation>
<rule name="required-entry" xsi:type="boolean">true</rule>
</validation>
<dataType>text</dataType>
<label translate="true">Title</label>
<dataScope>title</dataScope>
</settings>
</field>
</fieldset>
</form>
FormDataProvider Class:
<?php
declare(strict_types=1);
namespace SwiftsolutionsMobileAdminPanelModel;
use MagentoUiDataProviderAbstractDataProvider;
use MagentoFrameworkAppRequestInterface;
/**
* DataProvider component.
*/
class FormDataProvider extends AbstractDataProvider
{
/**
* @var array
*/
private $loadedData;
/**
* @var RequestInterface
*/
protected $request;
/**
* @param string $name
* @param string $primaryFieldName
* @param string $requestFieldName
* @param RequestInterface $request
* @param array $meta
* @param array $data
*/
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
RequestInterface $request,
array $meta = [],
array $data = []
) {
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
$this->request = $request;
}
/**
* @return array
*/
public function getData(): array
{
if (isset($this->loadedData)) {
return $this->loadedData;
}
// Set a default form_id (e.g., 1)
$formId = 1;
// Load your data here. For example:
$this->loadedData = [
$formId => [
'form_id' => $formId, // Populate the invisible form_id field
'title' => 'Default Value' // Populate the visible title field
]
];
return $this->loadedData;
}
/**
* Override addFilter to avoid collection usage.
*
* @param MagentoFrameworkApiFilter $filter
* @return void
*/
public function addFilter(MagentoFrameworkApiFilter $filter)
{
// Do nothing, as we don't need filtering for this data provider
}
/**
* Override getSearchResult to avoid collection usage.
*
* @return MagentoFrameworkApiSearchSearchResultInterface
*/
public function getSearchResult()
{
// Return an empty search result
return new MagentoFrameworkApiSearchSearchResult();
}
}
The Problem:
Despite the above implementation, the title field is not being populated with the Default Value. The form_id field is also not being set correctly. When I debug the request, I see that form_id is NULL.
Why the current implementation isn’t working.