1

I have added custom field to admin form using Ui Component.

Here is the XML to add field

<field name="all_keywords">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="dataScope" xsi:type="string">all_keywords</item>
            <item name="label" xsi:type="string" translate="true">All Keywords</item>
            <item name="dataType" xsi:type="string">text</item>
            <item name="formElement" xsi:type="string">input</item>
            <item name="source" xsi:type="string">all_keywords</item>
            <item name="validation" xsi:type="array">
                <item name="required-entry" xsi:type="boolean">true</item>
            </item>
            <item name="sortOrder" xsi:type="number">25</item>
        </item>
    </argument>
</field>

Field is displayed properly and it's showing data if I modified from db. Button on click on save button data not storing in db. I can see data in controller in post.

  • have you create all_keywords field with table? – Rakesh Jesadiya Oct 17 '17 at 11:54
  • @Rakesh No. I have used upgrade schema. table and form was already there – Kul Oct 17 '17 at 12:23
  • Is it a custom admin form or you extend a default Magento's form? – Nikolas Oct 17 '17 at 15:41
  • @Nicolas its custom form. Its installed modules form. – Kul Oct 17 '17 at 19:09
  • How did you set the data to entity model? Other fields will be stored in the database, except this field? – Khoa TruongDinh Oct 22 '17 at 15:06
2

I have found issue. actually it was custom paid module so i can't share code here. but they have used magento2's Api where they are defining set and get method for all field.

I thought to post this because it can help to other as well.

  • 1
    Yeah I had invalid code in my My\Module\Api\Data\GridInterface.php & My\Module\Model\Grid.php files, once I fixed this my values started populating on save etc. – BENN1TH Sep 22 '18 at 8:14
  • what invalid code you found? can you explain i also facing this problem – Chirag Patel Oct 29 '18 at 9:21
  • @ChiragPatel In magento2 there is interface in Api folder so you have to define this new field there if module is using it. in my case that was the issue. – Kul Oct 30 '18 at 12:37
  • @Kul Thanks for your time i got solution :) – Chirag Patel Oct 30 '18 at 13:09
0

If your form has the custom field displayed properly and the value you enter into it is properly passed to in the post data, then your ui component is working properly. Without knowing where you are posting the to, I cannot show where to edit your module. Most likely, the problem is that the api endpoint you are using doesn't know how to handle your custom field. If this is the case, you will need to extend the endpoint to include your custom field.

0

I can remember a common issue on M1 when you update the db schema but for some reason it don't get updated into the schema cache and then it doesn't get effect for the resource models, in that situation what you used to do is flush cache but in the hard way (rm -rf var/cache/* var/full_page_cache/*), for M2 it should be enough running this:

rm -rf var/cache/* var/generation/* var/page_cache/* var/di && php -f bin/magento cache:flush && php -f bin/magento setup:upgrade

If this doesn't solve your problem then you will have to debug into the save function if your custom model have your custom field actually set, if it's not set then it could happen because the POST params are being set directly "hardcoded" (ex. $model->setProductId( $request->getParam('product_id') )->set... ) into the model instead to use setData, in that situation the only way you have for set your custom field without rewrite the controller is by using the save_before event of that custom model into your custom module:

Once you have your custom module defined (I think you should already have it because you need it for add the new field) do this:

events.xml:

<event name="NAMESPACE_MODEL_save_before">
    <observer name="YOURNAMESPACE_CUSTOMMODULE::add_custom_value" instance="YOURNAMESPACE\CUSTOMMODULE\Observer\NamespaceModelSaveBefore" />
</event>

YOURNAMESPACE\CUSTOMMODULE\Observer\NamespaceModelSaveBefore.php:

<?php
namespace YOURNAMESPACE\CUSTOMMODULE\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Event\Observer;

/**
 * Class NamespaceModelSaveBefore
 *
 * @package YOURNAMESPACE\CUSTOMMODULE\Observer
 */
class NamespaceModelSaveBefore implements ObserverInterface
{
    /**
     * @var RequestInterface|null
     */
    protected $request = null;

    /**
     * NamespaceModelSaveBefore constructor.
     *
     * @param RequestInterface $request
     */
    public function __construct(
        RequestInterface $request
    ) {
        $this->request = $request;
    }

    /**
     * @param Observer $observer
     *
     * @return $this
     */
    public function execute(Observer $observer)
    {
        $namespaceModel = $observer->getEvent()->getDataObject();

        if ($customFieldValue = $this->request->getParam('YourCustomParam')) {
            $namespaceModel->setCustomField($customFieldValue);
        }

        return $this;
    }
}

Just in case, REMEMBER to replace all the NAMESPACE_MODEL, YOURNAMESPACE, CUSTOMMODULE, NamespaceModelSaveBefore, namespaceModel, YourCustomParam and setCustomField for what is needed for your case.

Kind Regards

0

*_form.xml file

<field name="price">
            <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">price</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">socks_color</item>
                    <item name="sortOrder" xsi:type="number">20</item>
                    <item name="dataScope" xsi:type="string">price</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>

After that check your Controller file

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

If your getting the required data or not in $data variable, if you get the required data it should store the values in database, if not please check the database table field name and the form field name must be match and also check the field data type.

If this does not work, then you should change the database field name :

<field name="field_name">

I think they must be conflicting.

<field name="all_keywords"> AND <item name="source" xsi:type="string">all_keywords</item>
-1

If you have changed code in installSchema.php then remove your module entry from

setup_module table and hit those two command and bang it will work.

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.