Skip to content

Magento UI component form not saving

I have a Magento 2 UI component form. Form loads without any errors but when I try to save it then it redirected back to same page with a parameter added as undefined. I had added var_dump die statement in the Save controller it seems it never enters the save controller. The url change from https://testing.k-sale.com/admin/managefaq/faq/new/ to https://testing.k-sale.com/admin/managefaq/faq/new/undefined

faq_form.xml

<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">faq_form.faq_form_data_source</item>
            <item name="deps" xsi:type="string">faq_form.faq_form_data_source</item>
        </item>
        <item name="label" xsi:type="string" translate="true">FAQ</item>
        <item name="config" xsi:type="array">
            <item name="dataScope" xsi:type="string">data</item>
            <item name="namespace" xsi:type="string">faq_form</item>
        </item>
        <item name="template" xsi:type="string">templates/form/collapsible</item>
    </argument>
    <settings>
        <buttons>
            <button name="back">
                <label>Back</label>
                <url path="*/*/index"/>
            </button>
            <button name="save" class="AveshFaqBlockAdminhtmlFaqEditButtonSave"/>
        </buttons>
        <dataScope>data</dataScope>
        <deps>
            <dep>faq_form.faq_form_data_source</dep>
        </deps>
    </settings>
    <dataSource name="faq_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="managefaq/faq/saves"/>
        </settings>
        <dataProvider class="AveshFaqUiDataProviderFaqFormDataProvider" name="faq_form_data_source">
            <settings>
                <requestFieldName>id</requestFieldName>
                <primaryFieldName>faq_id</primaryFieldName>
            </settings>
        </dataProvider>
    </dataSource>
    <fieldset name="general_information">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="collapsible" xsi:type="boolean">false</item>
                <item name="label" xsi:type="string" translate="true">General Information</item>
            </item>
        </argument>
        <field name="status">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">AveshFaqModelSourceFaqStatus</item>
                <item name="config" xsi:type="array">
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/form/element/select</item>
                    <item name="dataType" xsi:type="string">boolean</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="source" xsi:type="string">faq</item>
                    <item name="label" xsi:type="string" translate="true">Status</item>
                </item>
            </argument>
        </field>
        <field name="question">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Question</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">question</item>
                    <item name="sortOrder" xsi:type="number">20</item>
                    <item name="dataScope" xsi:type="string">question</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>
        <field name="answer">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="sortOrder" xsi:type="number">30</item>
                    <item name="rows" xsi:type="number">20</item>
                    <item name="formElement" xsi:type="string">textarea</item>
                    <item name="dataScope" xsi:type="string">answer</item>
                    <item name="source" xsi:type="string">faq</item>
                    <item name="label" xsi:type="string" translate="true">Answer</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

AveshFaqBlockAdminhtmlFaqEditButtonSave


/**
 *
 * @category  Custom Development
 * @email     [email protected]
 * @author    Avesh Naik
 * @website   learningmagento.com
 * @Date      31-07-2024
 */

namespace AveshFaqBlockAdminhtmlFaqEditButton;

use MagentoFrameworkViewElementUiComponentControlButtonProviderInterface;

class Save implements ButtonProviderInterface
{

    /**
     * Retrieve button-specified settings
     *
     * @return array
     */
    public function getButtonData()
    {
        return [
            'label' => __('Save'),
            'class' => 'save primary',
            'data_attribute' => [
                'mage-init' => ['button' => ['event' => 'save']],
                'form-role' => 'save',
            ],
        ];
    }
}

AveshFaqUiDataProviderFaqFormDataProvider

<?php

/**
 *
 * @category  Custom Development
 * @email     [email protected]
 * @author    Avesh Naik
 * @website   learningmagento.com
 * @Date      30-07-2024
 */

namespace AveshFaqUiDataProviderFaqForm;

use MagentoUiDataProviderAbstractDataProvider;
use AveshFaqModelResourceModelFaqCollection;

/**
 * Class DataProvider
 * @package AveshFaqUiDataProviderFaqForm
 */
class DataProvider extends AbstractDataProvider
{
    /**
     * @var array
     */
    protected $_loadedData;

    /**
     * @var AveshFaqModelResourceModelFaqCollection
     */
    protected $collection;

    /**
     * Dataprovider Constructor
     *
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param AveshFaqModelResourceModelFaqCollection $faqCollection
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        Collection $faqCollection
    ) {
        parent::__construct($name, $primaryFieldName, $requestFieldName);
        $this->collection = $faqCollection;
    }
    /**
     * Get data
     *
     * @return array
     */
    public function getData()
    {
        if (isset($this->_loadedData)) {
            return $this->_loadedData;
        }
        $items = $this->collection->getItems();
        foreach ($items as $faq) {
            $this->_loadedData[$faq->getId()] = $faq->getData();
        }
        return $this->_loadedData;
    }
}