Question

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.

Was it helpful?

Solution 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');
    }


}

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top