1

I am trying to add dynamic row using generic form class and it show like below. enter image description here

But the problem is that this value is not posting data.

Here is how i have implemented this. DynamicDiscount.php

    <?php

namespace Vendor\Module\Block\Adminhtml\Form\Field;

use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;

/**
 * Class AdditionalEmail
 */
class DynamicDiscount extends AbstractFieldArray
{
    /**
     * {@inheritdoc}
     */
    protected function _prepareToRender()
    {
        $this->addColumn('discount_label', ['label' => __('Discount Label'), 'class' => 'required-entry']);
        $this->addColumn('discount_per', ['label' => __('Percentage'), 'class' => 'required-entry']);
        $this->_addAfter = false;
        $this->_addButtonLabel = __('Add New Row');
    }


}

Form.php and _prepareLayout function

    $layoutBlock = $this->getLayout()->createBlock(
            \Vendor\Module\Block\Adminhtml\Form\Field\DynamicDiscount::class
        );

        $fieldsetRow = $form->addFieldset('discount_update', ['legend' => __('Layout Updates')]);
$fieldsetRow->addField('discount_data', 'note', []);
        $form->getElement('discount_update')->setRenderer($layoutBlock);

can anyone guide me please how to post this data and save in custom table and make it populated in form when edit it.

3
  • How Can I store this data to database and to show that data in Edit form? Please suggest.
    – Dolly
    Apr 4, 2023 at 15:31
  • You can serialized and save
    – Zahirabbas
    Apr 6, 2023 at 7:35
  • Thanks, I was able to serialize and save. Also able to show in Edit page. Now I have issue in delete buttons are not working for already loaded rows from database. If I add new then I am able to delete row. I get queryselector error for those rows delete button ids.
    – Dolly
    Apr 6, 2023 at 15:49

3 Answers 3

2

You should add name as config param when using addField and also you called setRenderer on fieldset element , if you check post request in console you'll see that values are passed but without name so you can't pick them up with $this->getRequest()->getParams() in controller, here's how it should look like:

        $fieldsetRow = $form->addFieldset(
            'discount_update',
            [
                'legend' => __('Layout Updates')
            ]
        );
        $fieldsetRow->addField(
            'discount_data',
            'note',
            [
                'name'=> 'discount_data' // element name so it can be properly passed with request
            ]
        )->setRenderer($layoutBlock);

\Magento\Framework\Data\Form\Element\Fieldset::addField passes back \Magento\Framework\Data\Form\Element\AbstractElement instance on which you can call setRenderer directly, so you can skip $form->getElement('discount_data')->setRenderer($layoutBlock);

If for whatever reason you do need to call it like that, make sure that you are passing field id and not the fieldset id, in your example field id would be discount_data

3
  • Yes this worked but now how to save and setdata when edit form and populate with saved data.I have tried with this $form->addValues( [ 'id' => $customerGroup->getId(), 'customer_group_code' => $customerGroup->getCode(), 'tax_class_id' => $defaultCustomerTaxClass, "discount_data['discount_data']" => ['discount_label'=>'4545','discount_per'=>'45'] ] ); but not populated in form
    – Zahirabbas
    Aug 12, 2020 at 8:30
  • That's an entirely different question, I don't know what exactly are you doing with it, is it a custom entity or are you extending some core feature, in either case you would need set data to model and save it Aug 12, 2020 at 8:33
  • Yes true I am adding this feature to customer_group form and in that value is populating with addValues function.I have already set but not working
    – Zahirabbas
    Aug 12, 2020 at 8:34
2

Here is the full answer it may help someone.Might there is some good solution but for now it works.Please feel free to answer better solution.

in the Form.php _prepareLayout function should be extended from \Magento\Backend\Block\Widget\Form\Generic

    $fieldsetRow = $form->addFieldset('discount_update', ['legend' => __('Discount Part')]);
        $dynamicRow = $fieldsetRow->addField('discount_data', 'text', array(
            'name'      => 'discount_data',
            'required'  => false,
        ))->setRenderer($layoutBlock);
/* set data here when edit from your custom table */
        $dynamicRow->setValue(array(array('discount_label'=>'4545','discount_per'=>'45')));

Then create renderer file in my case it is DynamicDiscount.php

    <?php

namespace Vendor\Module\Block\Adminhtml\Form\Field;

use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;

/**
 * Class DynamicDiscount
 */
class DynamicDiscount extends AbstractFieldArray
{
    /**
     * {@inheritdoc}
     */
    protected function _prepareToRender()
    {
        $this->addColumn('discount_label', ['label' => __('Discount Label'), 'class' => 'required-entry  admin__control-text']);
        $this->addColumn('discount_per', ['label' => __('Percentage'), 'class' => 'required-entry validate-digits-range digits-range-1-100 admin__control-text']);
        $this->_addAfter = false;
        $this->_addButtonLabel = __('Add New Row');
    }


}
4
  • How to show saved data in the grid there?
    – amitshree
    Jun 8, 2021 at 8:08
  • 1
    $dynamicRow->setValue(array(array('discount_label'=>'4545','discount_per'=>'45'))); by setting this to form
    – Zahirabbas
    Jun 8, 2021 at 8:46
  • @Zaheerabbas Thanks for your answer and it is working as expected. My doubt is how can we add drag and drop to change the position in this dynamic rows ? Jun 15, 2021 at 11:49
  • @Zaheerabbas Above solution showing me dynamic rows in form but not getting in Save.php controller in getPostData. How can I get data in Save.php?
    – Dolly
    Apr 4, 2023 at 15:22
0

Add Class as below to render dynamic rows section:

<?php
namespace Vendor\Module\Block\Adminhtml\Form\Field;
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;

/**
 * Class AdditionalEmail
 */
class DynamicDiscount extends AbstractFieldArray
{
    /**
     * {@inheritdoc}
     */
    protected function _prepareToRender()
    {
        $this->addColumn('discount_label', ['label' => __('Discount Label'), 'class' => 'required-entry']);
        $this->addColumn('discount_per', ['label' => __('Percentage'), 'class' => 'required-entry']);
        $this->_addAfter = false;
        $this->_addButtonLabel = __('Add New Row');
    }
}

Form.php and _prepareLayout function

$fieldsetRow = $form->addFieldset(
        'discount_update',
        [
            'legend' => __('Layout Updates')
        ]
    );
    $fieldsetRow->addField(
        'discount_data',
        'note',
        [
            'name'=> 'discount_data' // element name so it can be properly passed with request
        ]
    )->setRenderer($layoutBlock);

Now to display dynamic rows data after save in edit form you need to set data in registry for dynamic rows field in edit controller action as below:

 $discountData['_0'] = [
     'discount_label'=>'4545',
     'discount_per'=>'45'
];
$discountData['_1'] = [
     'discount_label'=>'4646',
     'discount_per'=>'46'
];
$formData['discount_data'] = $discountData;
$this->_registry->register($formData);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.