1

I still confusd abaout the magento 2. I'am new in magento. my question is How to sending data from the layout form to the controller ?

this my code ui_component/ppp.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">ppp_duplicateStore_form.ppp_duplicateStore_form_data_source</item>
    </item>
    <item name="deps" xsi:type="string">ppp_duplicateStore_form.ppp_duplicateStore_form_data_source</item>
    <item name="namespace" xsi:type="string">ppp_duplicateStore_form</item>
    <item name="label" xsi:type="string" translate="true">Store list</item>
    <item name="template" xsi:type="string">templates/form/collapsible</item>
    <item name="buttons" xsi:type="array">
        <item name="saveDuplicateStore" xsi:type="string">Ppp\DuplicateStore\Block\Adminhtml\Duplicate\Store\save</item>
    </item>
</argument>

<dataSource name="ppp_duplicateStore_form_data_source">
    <argument name="dataProvider" xsi:type="configurableObject">
        <argument name="class" xsi:type="string">Ppp\DuplicateStore\Model\DataProvider</argument>
        <argument name="name" xsi:type="string">ppp_duplicateStore_form_data_source</argument>
        <argument name="primaryFieldName" xsi:type="string">store_id</argument>
        <argument name="requestFieldName" xsi:type="string">store_id</argument>
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="submit_url" xsi:type="url" path="ppp/duplicate_store/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>
    <settings>
        <label/>
    </settings>
     <field name="store" formElement="select">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="source" xsi:type="string">  </item>
            </item>
        </argument>
        <settings>
            <validation>
                <rule name="required-entry" xsi:type="boolean">true</rule>
            </validation>
            <!-- <elementTmpl>ui/grid/filters/elements/ui-select</elementTmpl> -->
            <dataType>text</dataType>
            <label translate="true">select store</label>
            <dataScope>store</dataScope>
        </settings>
        <formElements>
            <select>
                <settings>
                    <options class="Ppp\DuplicateStore\Model\Source\Store"/>
                </settings>
            </select>
        </formElements>
    </field>
</fieldset>

And this my Controller/Adminhtml/Duplicate/Store/Save.php

<?php
namespace Ppp\DuplicateStore\Controller\Adminhtml\Duplicate\Store;
class Save {
    public function execute() {
       echo json_encode("test is true");exit();
    }
}

this my DuplicateStore/Block/Adminhtml/Duplicate/Store/Save.php

<?php
 namespace Ppp\DuplicateStore\Block\Adminhtml\Duplicate\Store;
 use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

 class Save implements ButtonProviderInterface
 {
 /**
 * @return array
 */
 public function getButtonData()
 {
    return [
        'label' => __('Save kri'),
        'class' => 'save primary',
        'data_attribute' => [
            'mage-init' => [
                'buttonAdapter' => [
                    'actions' => [
                        [
                            'targetName' => 'ppp_duplicateStore_form.ppp_duplicateStore_form',
                            'actionName' => 'save',
                            'params' => [
                                true
                            ]
                        ]
                    ]
                ]
            ]
        ],
        'sort_order' => 40,
    ];
}}

save button inside ui_component/ppp.xml

<item name="saveDuplicateStore" xsi:type="string">Ppp\DuplicateStore\Block\Adminhtml\Duplicate\Store\save</item>

I tried to show out the text inside json_encode but when I clicked the saveDuplicateStore button only showed 404 not found..

0

You need to change get button data code like:

return [
    'label' => __('Save kri'),
    'class' => 'save primary',
    'data_attribute' => [
        'mage-init' => ['button' => ['event' => 'save']],
        'form-role' => 'save',
    ],
    'sort_order' => 40,
];

After that you need to update your xml part code of 'save' instead of 'Save'.

<?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">ppp_duplicateStore_form.ppp_duplicateStore_form_data_source</item>
    </item>
    <item name="deps" xsi:type="string">ppp_duplicateStore_form.ppp_duplicateStore_form_data_source</item>
    <item name="namespace" xsi:type="string">ppp_duplicateStore_form</item>
    <item name="label" xsi:type="string" translate="true">Store list</item>
    <item name="template" xsi:type="string">templates/form/collapsible</item>
    <item name="buttons" xsi:type="array">
        <item name="saveDuplicateStore" xsi:type="string">Ppp\DuplicateStore\Block\Adminhtml\Duplicate\Store\Save</item>
    </item>
</argument>

<dataSource name="ppp_duplicateStore_form_data_source">
    <argument name="dataProvider" xsi:type="configurableObject">
        <argument name="class" xsi:type="string">Ppp\DuplicateStore\Model\DataProvider</argument>
        <argument name="name" xsi:type="string">ppp_duplicateStore_form_data_source</argument>
        <argument name="primaryFieldName" xsi:type="string">store_id</argument>
        <argument name="requestFieldName" xsi:type="string">store_id</argument>
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="submit_url" xsi:type="url" path="ppp/duplicate_store/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>
    <settings>
        <label/>
    </settings>
     <field name="store" formElement="select">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="source" xsi:type="string">  </item>
            </item>
        </argument>
        <settings>
            <validation>
                <rule name="required-entry" xsi:type="boolean">true</rule>
            </validation>
            <!-- <elementTmpl>ui/grid/filters/elements/ui-select</elementTmpl> -->
            <dataType>text</dataType>
            <label translate="true">select store</label>
            <dataScope>store</dataScope>
        </settings>
        <formElements>
            <select>
                <settings>
                    <options class="Ppp\DuplicateStore\Model\Source\Store"/>
                </settings>
            </select>
        </formElements>
    </field>
</fieldset>
  • Hi @Kamlesh Solanki I have change it and I got an error Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid XML in file C: *update sorry I am forget to close form tag – kris Apr 10 at 8:16
  • Hi @Kamlesh Solanki thanks but its still showing 404 not found. – kris Apr 10 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.