cancel
Showing results for 
Search instead for 
Did you mean: 

Admin Edit Form MultiSelect Options

Admin Edit Form MultiSelect Options

Hello,

 I have a system.xml file as follows. Now i need same form in customer edit form. For that i created the following file , which is displaying form fields in tabs finely but with out any values in it. I tried the following code to add a category dropdown field.

 

System.XML

 

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        
        <section id="masspriceupdater_setup" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1"
                 showInStore="1">
            <class>separator-top</class>
            <label>AV MassPriceUpdater - Price Configuration</label>
            <tab>catalog</tab>
            <resource>AV_MassPriceUpdater::masspriceupdater_config</resource>
            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1"
                   showInStore="1">
                <label>General Configuration</label>
                <field id="category_list" translate="label" type="multiselect" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Category / Categories</label>
                    <comment>
                        <![CDATA[Select category / categories for price updater <br>
                           Selected category products price will be updated]]>
                    </comment>
                    <validate>required-entry</validate>
                    <source_model>AV\MassPriceUpdater\Model\Config\Source\CategoryList</source_model>
                </field>
                <field id="price_attribute" translate="label" type="multiselect" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Price type(s)</label>
                    <comment>
                        <![CDATA[Select price type(s) <br>
                           Selected price type(s) will be updated]]>
                    </comment>
                    <validate>required-entry</validate>
                    <source_model>AV\MassPriceUpdater\Model\Config\Source\PriceAttribute</source_model>
                </field>
                <field id="updater_type" translate="label" type="select"
                       sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Update type</label>
                    <comment><![CDATA[Here you can select the update type <br>
                            <strong>If you select 'fixed', please fill the field price</strong><br>
                            <strong>If you select 'percentage', please fill the field percentage</strong>]]></comment>
                    <validate>required-entry</validate>
                    <source_model>AV\MassPriceUpdater\Model\Config\Source\PriceType</source_model>
                </field>
                <field id="price" translate="label" type="text" sortOrder="40" showInDefault="1"
                       showInWebsite="1"
                       showInStore="1">
                    <label>Price</label>
                    <comment>
                        <![CDATA[Set amount, negative or positive]]></comment>
                </field>
                <field id="percentage" translate="label" type="text" sortOrder="50" showInDefault="1"
                       showInWebsite="1"
                       showInStore="1">
                    <label>Percentage</label>
                    <comment>
                        <![CDATA[Set percentage, negative or positive <br>
                            For example: -20% or 20%]]></comment>
                </field>
            </group>
        </section>
    </system>
</config>

Form Fields Code I am Trying

 

public function _prepareForm(){
		$form = $this->_formFactory->create(
			[
				'data' => [
					'id'     => 'priceupdate_form',
					'action' => $this->getData('action'),
					'method' => 'post',
					'enctype'=> 'multipart/form-data'
				]
			]
		);

		$fieldset = $form->addFieldset(
			'base_fieldset',
			['legend' => __('General')]
		);
 
		
		

		$fieldset->addField(
			'category_list',
			'multiselect',
			[
				'name' => 'category_list',
				'label'	=> __('Category / Categories'),
				'required' => false,
				'renderer' => 'AV\MassPriceUpdater\Model\Config\Source\CategoryList'
			]
		);

		$fieldset->addField(
			'price_attribute',
			'multiselect',
			[
				'name' => 'price_attribute',
				'label'	=> __('Price type(s)'),
				'required' => false,
				'renderer'        => 'AV\MassPriceUpdater\Model\Config\Source\PriceAttribute'
			]
		);


		$fieldset->addField(
			'job',
			'text',
			[
				'name' => 'job',
				'label'	=> __('Job'),
				'required' => false
			]
		);



		

		$form->setUseContainer(true);
		$this->setForm($form);
 
		return parent::_prepareForm();
	}

My Category List Modal file is like below

<?php

namespace AV\MassPriceUpdater\Model\Config\Source;

use Magento\Framework\Option\ArrayInterface;

class CategoryList implements ArrayInterface
{
    protected $_categoryHelper;

    public function __construct(\Magento\Catalog\Helper\Category $catalogCategory)
    {
        $this->_categoryHelper = $catalogCategory;
    }

    /*
    *   Return categories helper
    */

    public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true)
    {
        return $this->_categoryHelper->getStoreCategories($sorted, $asCollection, $toLoad);
    }

    /*
    *   Option getter
    *   @return array
    */
    public function toOptionArray()
    {
        $arr = $this->toArray();
        $ret = [];

        foreach ($arr as $key => $value) {

            $ret[] = [
                'value' => $key,
                'label' => $value
            ];
        }

        return $ret;
    }

    /*
    *   Get options in "key-value" format
    *   @return array
    */
    public function toArray()
    {
        $categories = $this->getStoreCategories(true, false, true);

        $catagoryList = array();
        foreach ($categories as $category) {

            $catagoryList[$category->getEntityId()] = __($category->getName());
        }

        return $catagoryList;
    }

}

When ever vendor saves the data of above form , I want to store the information in another table using Ajax method. Can you suggest me a solution for this?