0

I have a form like this. This is used to save a new custom admin. But I have a problem with showing a value in the store_id field while editing. form for editing

While showing this form the store field must be selected. The name of this field is store_id but I have to show the name of the store instead. so I need to make a class for getting the store's name. I can get all the names of the stores and use them for the dropdown list. But after seeing the detail when editing, the store_id field is empty and doesn't even show the store_id or the store_name. However, the store_id can be saved to the database.

This is the part for the store_id field at admin_form.xml

<field name="store_id">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Module\Vendor\Model\Select\Source\Store</item>
        <item name="config" xsi:type="array">
            <item name="componentType" xsi:type="string">field</item>
            <item name="formElement" xsi:type="string">select</item>
            <item name="label" xsi:type="string">Store</item>
            <item name="visible" xsi:type="boolean">true</item>
            <item name="dataType" xsi:type="string">text</item>
            <item name="source" xsi:type="string">Customer</item>
            <item name="dataScope" xsi:type="string">store_id</item>
        </item>
    </argument>
</field>

and this is the class for showing the name of the store

<?php

class Store extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{

    /**
     * @var \Vendor\Module\Model\CustomFactory
     */
    private $customFactory;

    /**
     * @param \Magento\Customer\Model\CustomerFactory $customerFactory
     */
    public function __construct(\Module\Vendor\Model\ResourceModel\Store\CollectionFactory $customFactory)
    {
        $this->customFactory = $customFactory;
    }

    /**
     * Get all options
     *
     * @return array
     */
    public function getAllOptions()
    {
        $customCollection = $this->customFactory->create();

        $customCollection->addFieldToFilter('status', 'Active');

        $customCollection->setOrder('store_name', 'ASC');

        $this->_options = [['label' => 'Please select', 'value' => '']];

        foreach ($customCollection as $custom) {
            $this->_options[] = ['label' => $custom->getData('store_name'), 'value' => $custom->getData('id')];
        }
        return $this->_options;
    }

     
}

How can I make this field is selected based on the store_id and the selected field shows the store's name?

4
  • In your form dataProvider class, just before return statement, echo your returning data and exit to see if store_id is actually set there or not. – Abdul Samad Abbasi Jan 20 at 9:16
  • Thank you so much you are absolutely right that the store_id does not exist in the array for loading the data. so I need to make adjustments to the array. I inject the store factory get the store_id, and insert this store_id to the array like this $this->loadedData[$model->getId()]["store_id"] = $store_id; – Friska Sianturi Jan 21 at 4:51
  • Yes I was going to tell you this, glad that you did it yourself, must be good learning, cheers! Still answering for others to help :) – Abdul Samad Abbasi Jan 21 at 5:52
  • Good luck then! it will be very helpful – Friska Sianturi Jan 22 at 2:42
1

The problem is because store_ids are not from same table as your custom model, so it doesn't get loaded, you need to set in manually, in either _afterLoad of your model or DataProvider class to set it in array with same array index as the field name in your form and you're good to go.

0

The problem is in your Store class, it must be implemented from Magento\Framework\Data\OptionSourceInterface.

Then replace the getAllOptions function with toOptionArray, the content of the new function function is

public function toOptionArray(): array
    {
        $result = [];

        $customCollection = $this->customFactory->create();
        $customCollection->addFieldToFilter('status', 'Active');
        $customCollection->setOrder('store_name', 'ASC');

        $result[] = [
            'value' => '',
            'label' => 'Please select'
        ];
        
        foreach ($customCollection as $custom) {
            $result[] = [
                'value' => $custom->getData('id'),
                'label' => $custom->getData('store_name')
            ];
        }


        return $result;
    }

And you must make sure the column name in your database and the field name in your UI component file is the same store_id .

1
  • thank you. I have tried this and the result is the same. The field is not selected. The field name already the same as the field name in the table. I checked the field name by using inspect just want to make sure and the name is store_id; – Friska Sianturi Jan 20 at 11:26

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.