6

I am getting this error while editing my form.

This is my code :

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<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">zipcode_form.zipcode_form_data_source</item>
            <item name="deps" xsi:type="string">zipcode_form.zipcode_form_data_source</item>
            <item name="namespace" xsi:type="string">zipcode_form</item>
        </item>
        <item name="label" xsi:type="string" translate="true">General Information</item>

          <item name="layout" xsi:type="array">
            <item name="type" xsi:type="string">tabs</item>
            <item name="navContainerName" xsi:type="string">left</item>
        </item>

        <item name="buttons" xsi:type="array">
            <item name="back" xsi:type="string">RB\CheckDelivery\Block\Adminhtml\Zipcode\Edit\BackButton</item>
            <item name="delete" xsi:type="string">RB\CheckDelivery\Block\Adminhtml\Zipcode\Edit\DeleteButton</item>
            <item name="reset" xsi:type="string">RB\CheckDelivery\Block\Adminhtml\Zipcode\Edit\ResetButton</item>
            <item name="save" xsi:type="string">RB\CheckDelivery\Block\Adminhtml\Zipcode\Edit\SaveButton</item>
            <item name="save_and_continue" xsi:type="string">RB\CheckDelivery\Block\Adminhtml\Zipcode\Edit\SaveAndContinueButton</item>
        </item>
    </argument>

    <dataSource name="zipcode_form_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">RB\CheckDelivery\Ui\DataProvider\ZipcodeDataProvider</argument>
            <argument name="name" xsi:type="string">zipcode_form_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">zipcode_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="checkdelivery/zipcode/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="zipcodes">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">ZIP Code Information</item>
            </item>
        </argument>


        <field name="zipcode_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">zipcodes</item>
                    <item name="dataScope" xsi:type="string">zipcode_id</item>
                </item>
            </argument>
        </field>

        <field name="status">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">boolean</item>
                    <item name="label" xsi:type="string" translate="true">Enable Zipcode</item>
                    <item name="formElement" xsi:type="string">checkbox</item>
                    <item name="source" xsi:type="string">zipcodes</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                    <item name="dataScope" xsi:type="string">status</item>
                    <item name="prefer" xsi:type="string">toggle</item>
                    <item name="valueMap" xsi:type="array">
                        <item name="true" xsi:type="number">1</item>
                        <item name="false" xsi:type="number">0</item>
                    </item>
                    <item name="default" xsi:type="number">1</item>
                </item>
            </argument>
        </field>

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

         <field name="country_id">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Magento\Directory\Model\Config\Source\Country</item>
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Country</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/form/element/country</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="source" xsi:type="string">zipcodes</item>
                    <!--<item name="dataScope" xsi:type="string">country_id</item>-->
                    <item name="imports" xsi:type="array">
                        <item name="default" xsi:type="string">${ $.provider }:data.customer.website_id</item>
                    </item>
                </item>
            </argument>
        </field>

        <field name="city">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">City</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">zipcodes</item>
                </item>
            </argument>
        </field> 

<!--         <field name="region">
            <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">address</item>
                </item>
            </argument>
        </field>-->

<!--        <field name="region_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="source" xsi:type="string">address</item>
                    <item name="customEntry" xsi:type="string">region</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                    <item name="filterBy" xsi:type="array">
                        <item name="target" xsi:type="string">${ $.provider }:${ $.parentScope }.country_id</item>
                        <item name="field" xsi:type="string">country_id</item>
                    </item>
                </item>
            </argument>
        </field>-->

    </fieldset>
</form>

ZipcodeDataProvider :-

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace RB\CheckDelivery\Ui\DataProvider;

use RB\CheckDelivery\Model\ResourceModel\Zipcode\CollectionFactory;
use Magento\Framework\App\Request\DataPersistorInterface;
/**
 * Class ProductDataProvider
 */
class ZipcodeDataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{

    /**
     * @var DataPersistorInterface
     */
    private $dataPersistor;

    /**
     * Product collection
     *
     * @var \Magento\Catalog\Model\ResourceModel\Product\Collection
     */
    protected $collection;

    /**
     * @var \Magento\Ui\DataProvider\AddFieldToCollectionInterface[]
     */
    protected $addFieldStrategies;

    /**
     * @var \Magento\Ui\DataProvider\AddFilterToCollectionInterface[]
     */
    protected $addFilterStrategies;

        /**
     * @var array
     */
    protected $loadedData;

    /**
     * Construct
     *
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param CollectionFactory $collectionFactory
     * @param DataPersistorInterface $dataPersistor
     * @param \Magento\Ui\DataProvider\AddFieldToCollectionInterface[] $addFieldStrategies
     * @param \Magento\Ui\DataProvider\AddFilterToCollectionInterface[] $addFilterStrategies
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $collectionFactory,
        DataPersistorInterface $dataPersistor,
        array $addFieldStrategies = [],
        array $addFilterStrategies = [],
        array $meta = [],
        array $data = []
    ) {
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
        $this->collection = $collectionFactory->create();
        $this->addFieldStrategies = $addFieldStrategies;
        $this->addFilterStrategies = $addFilterStrategies;
        $this->dataPersistor = $dataPersistor;
    }

    /**
     * Get data
     *
     * @return array
     */
//    public function getData()
//    {
//        if (!$this->getCollection()->isLoaded()) {
//            $this->getCollection()->load();
//        }
//        $items = $this->getCollection()->toArray();
////        echo '<pre>';
////        print_r(array_values($items));
////        die;
//
////        return $items;
//        return [
//            'totalRecords' => $this->getCollection()->getSize(),
//            'items' => array_values($items),
//        ];
//    }

//     

    /**
     * Add field to select
     *
     * @param string|array $field
     * @param string|null $alias
     * @return void
     */
    public function addField($field, $alias = null)
    {
        if (isset($this->addFieldStrategies[$field])) {
            $this->addFieldStrategies[$field]->addField($this->getCollection(), $field, $alias);
        } else {
            parent::addField($field, $alias);
        }
    }
}
  • add the content of the data provider class RB\CheckDelivery\Ui\DataProvider\ZipcodeDataProvider – Marius Jan 3 '17 at 8:36
  • @Marius : I have added the code for RB\CheckDelivery\Ui\DataProvider\ZipcodeDataProvider – Sanjay Chaudhary Jan 3 '17 at 8:47
  • @Marius : I guess there is no way to build country, region functionality using UI components, so I have moved to block concept after all they are easy to understand and manage. – Sanjay Chaudhary Jan 5 '17 at 7:06
4

Inside on your module

yourmodule/Model/Grid/DataProvider.php

You have to place this below getData() function

public function getData()
{
    if (isset($this->loadedData)) {
        return $this->loadedData;
    }
    $items = $this->collection->getItems();
    foreach ($items as $model) {
        $this->loadedData[$model->getId()] = $model->getData();
    }
    $data = $this->dataPersistor->get('your_module_variable');

    if (!empty($data)) {
        $model = $this->collection->getNewEmptyItem();
        $model->setData($data);
        $this->loadedData[$model->getId()] = $model->getData();
        $this->dataPersistor->clear('your_module_variable');
    }

    return $this->loadedData;
}
| improve this answer | |
4

I found the answer a long ago but I guess forgot to share it so I'am updating it considering it might help someone.

So below is the solution :

All you have to do is add given code in your model collection file.

protected $_idFieldName = 'block_id'; // Set primary key of your table here..

For reference please check this file in core modules : /var/www/html/magento2/vendor/magento/module-cms/Model/ResourceModel/Block/Collection.php

| improve this answer | |
  • Thanks for the update :) it will definitely help future readers ! – Aditya Shah Mar 22 '19 at 12:58

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.