简体   繁体   中英

How to get data from ui component form to controller in Magento 2

I'm trying to get data from a form to my controller. I'm using getRequest()->getParams() in my controller, but all I'm getting is the form key. What am I doing wrong?

The var_dump echoes out an array containing the key and form_key, and nothing else.

Controller:

    $data = $this->getRequest()->getParams();
    var_dump($data);

Block SaveButton:

public function getButtonData()
{
    return [
        'label' => __('Save'),
        'class' => 'save primary',
        'data_attribute' => [
            'mage-init' => ['button' => ['event' => 'save']],
            'form-role' => 'save',
        ],
        'sort_order' => 90
    ];

Form, from ui_component:

<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">vendor_colorsort_add_new_mapping.vendor_colorsort_all_mappings_data_source</item>
            <item name="deps" xsi:type="string">vendor_colorsort_add_new_mapping.vendor_colorsort_all_mappings_data_source</item>
        </item>
        <item name="label" xsi:type="string" translate="true">Add New Mapping</item>
        <item name="template" xsi:type="string">templates/form/collapsible</item>
    </argument>
    <settings>
        <buttons>
            <button name="save" class="vendor\ColorSort\Block\Adminhtml\Form\Edit\SaveButton" />
        </buttons>
        <deps>
            <dep>vendor_colorsort_add_new_mapping.vendor_colorsort_all_mappings_data_source</dep>
        </deps>
    </settings>
    <dataSource name="vendor_colorsort_all_mappings_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
           <argument name="class" xsi:type="string">Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider</argument>
           <argument name="name" xsi:type="string">vendor_colorsort_all_mappings_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">id</argument>
            <argument name="requestFieldName" xsi:type="string">mapping</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
                    <item name="submit_url" xsi:type="url" path="*/*/save" />
                    <item name="storageConfig" xsi:type="array">
                        <item name="indexField" xsi:type="string">id</item>
                    </item>
                </item>
            </argument>
        </argument>
    </dataSource>
    <fieldset name="mapping_form">
        <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">Add New Mapping</item>
            </item>
        </argument>
        <field name="mapping">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Mapping</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="dataScope" xsi:type="string">mapping</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

请用:

   $data = $this->getRequest()->getPostValue();

add datascope in setting

<settings>
        <buttons>
            <button name="save" class="vendor\ColorSort\Block\Adminhtml\Form\Edit\SaveButton" />
        </buttons>
        <deps>
            <dep>vendor_colorsort_add_new_mapping.vendor_colorsort_all_mappings_data_source</dep>
        </deps>
        <dataScope>data</dataScope>
    </settings>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM