0

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="Avesh\Faq\Block\Adminhtml\Faq\Edit\Button\Save"/>
        </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/save"/>
        </settings>
        <dataProvider class="Avesh\Faq\Ui\DataProvider\Faq\Form\DataProvider" 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">Avesh\Faq\Model\Source\Faq\Status</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>

Avesh\Faq\Block\Adminhtml\Faq\Edit\Button\Save


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

namespace Avesh\Faq\Block\Adminhtml\Faq\Edit\Button;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

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',
            ],
        ];
    }
}

Avesh\Faq\Ui\DataProvider\Faq\Form\DataProvider

<?php

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

namespace Avesh\Faq\Ui\DataProvider\Faq\Form;

use Magento\Ui\DataProvider\AbstractDataProvider;
use Avesh\Faq\Model\ResourceModel\Faq\Collection;

/**
 * Class DataProvider
 * @package Avesh\Faq\Ui\DataProvider\Faq\Form
 */
class DataProvider extends AbstractDataProvider
{
    /**
     * @var array
     */
    protected $_loadedData;

    /**
     * @var \Avesh\Faq\Model\ResourceModel\Faq\Collection
     */
    protected $collection;

    /**
     * Dataprovider Constructor
     *
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param \Avesh\Faq\Model\ResourceModel\Faq\Collection $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;
    }
}

Save Controller

<?php

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

namespace Avesh\Faq\Controller\Adminhtml\Faq;

use Magento\Backend\App\Action\Context;
use Magento\Framework\App\ResponseInterface;

/**
 * Class Save
 * @package Avesh\Faq\Controller\Adminhtml\Faq
 */
class Save extends \Magento\Backend\App\Action
{

    protected $_faqRepository;

    protected $_faqFactory;

    public function __construct(
        \Avesh\Faq\Api\FaqRepositoryInterface $faqRepository,
        \Avesh\Faq\Api\Data\FaqInterfaceFactory $faqFactory,
        Context $context
    ) {
        $this->_faqRepository = $faqRepository;
        $this->_faqFactory = $faqFactory;
        parent::__construct($context);
    }

    /**
     * Execute action based on request and return result
     *
     * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
     * @throws \Magento\Framework\Exception\NotFoundException
     */
    public function execute()
    {
        var_dump("herere"); die;
        try {
            $params = $this->getRequest()->getParams();
            $faq = $this->_faqFactory->create();
            if(isset($data["id"]) && $data["id"]){
                $faq = $this->_faqRepository->getById($data["id"]);
            }
            $faq->setAnswer($params["answer"])
                ->setQuestion($params["question"])
                ->setStatus($params["status"]);
            $this->_faqRepository->save($faq);
        }
        catch (\Exception $exception) {
            $this->messageManager->addErrorMessage(__("Something went wrong while saving it"));
        }
        $redirect = $this->resultRedirectFactory->create();
        return $redirect->setPath('*/*/index');
    }
}
2
  • Save controller or Saves ? Commented Aug 1, 2024 at 7:48
  • Let me update the post. The thing is its not passing there at all Commented Aug 1, 2024 at 8:52

2 Answers 2

2

Try to Update the code as below :

faq_form.xml

<?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">faq_form.faq_form_data_source</item>
        </item>
        <item name="config" xsi:type="array">
            <item name="namespace" xsi:type="string">faq_form</item>
            <item name="deps" xsi:type="string">faq_form.faq_form_data_source</item>
            <item name="dataScope" xsi:type="string">data</item>
        </item>
        <item name="label" xsi:type="string" translate="true">FAQ</item>
        <item name="template" xsi:type="string">templates/form/collapsible</item>
        <item name="buttons" xsi:type="array">
            <item name="back" xsi:type="string">Avesh\Faq\Block\Adminhtml\Faq\Edit\Button\Back</item>
            <item name="faq_save" xsi:type="string">Avesh\Faq\Block\Adminhtml\Faq\Edit\Button\Save</item>
        </item>
    </argument>
    <dataSource name="faq_form_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Avesh\Faq\Ui\DataProvider\Faq\Form\DataProvider</argument>
            <argument name="name" xsi:type="string">faq_form_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">id</argument>
            <argument name="requestFieldName" xsi:type="string">id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="submit_url" xsi:type="url" path="managefaq/faq/save"/>
                </item>
            </argument>
        </argument>
        <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>
    </dataSource>
    <fieldset name="general_information">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">General Information</item>
                <item name="collapsible" xsi:type="boolean">false</item>
            </item>
        </argument>
        <field name="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">faq_form_data_source</item>
                </item>
            </argument>
        </field>
        <field name="status">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Avesh\Faq\Model\Source\Faq\Status</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_form_data_source</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">faq_form_data_source</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_form_data_source</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>

Avesh\Faq\Block\Adminhtml\Faq\Edit\Button\Save.php

<?php

namespace Avesh\Faq\Block\Adminhtml\Faq\Edit\Button;

use Magento\Customer\Block\Adminhtml\Edit\GenericButton;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

class Save extends GenericButton 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',
            ],
            'sort_order' => 90,
            'on_click' => sprintf("location.href = '%s';", $this->getBackUrl())
        ];
    }

    public function getBackUrl()
    {
        return $this->getUrl('managefaq/faq/save');
    }
}

Avesh\Faq\Block\Adminhtml\Faq\Edit\Button\Back.php

<?php

namespace Avesh\Faq\Block\Adminhtml\Faq\Edit\Button;

use Magento\Backend\Block\Widget\Container;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

class Back extends Container implements ButtonProviderInterface
{
    /**
     * Retrieve button-specified settings
     *
     * @return array
     */
    public function getButtonData()
    {
        return [
            'label' => __('Back'),
            'on_click' => sprintf("location.href = '%s';", $this->getBackUrl()),
            'class' => 'back',
            'sort_order' => 10
        ];
    }

    public function getBackUrl()
    {
        return $this->getUrl('*/*/index');
    }
}

After Updating the files, Please run the compile and static-content deploy command.

3
  • Same problem. I am running magento 2.4.7 on a docker env. Commented Aug 1, 2024 at 18:30
  • @AveshNaik If possible can you send me the module via dropBox or google drive, So that I can check Commented Aug 2, 2024 at 10:17
  • I have updated in the file faq_form.xml, Please check @Avesh Commented Aug 2, 2024 at 14:07
0

In faq_form.xml

In submit <submitUrl path="managefaq/faq/saves"/>

you are passing saves which should be save please update and try with this

<submitUrl path="managefaq/faq/save"/>
1
  • I checked the code and changed the name to save. Still same Commented Aug 1, 2024 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.