Skip to main content
deleted 2298 characters in body
Source Link
Sohel Rana
  • 36.1k
  • 3
  • 73
  • 91

Check updated code, it should workCreate a di. Don't forget to clear cachexml under adminhtml and add following code:

Basically need to overwrite Magento\Backend\Block\Widget\Form\Element\Dependence class


namespace Siarhey\Test\Block\Adminhtml\Promo\Quote\Edit\Tab;

class Actions extends \Magento\Backend\Block\Widget\Form\Generic<preference implementsfor="Magento\Backend\Block\Widget\Form\Element\Dependence"
    \Magento\Backend\Block\Widget\Tab\TabInterface
{
/**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registrytype="Vendor\Module\Block\Widget\Form\Element\Dependence" $registry/>
     * @param \Magento\Framework\Data\FormFactory $formFactory 


     * @param arraynamespace $dataVendor\Module\Block\Widget\Form\Element;
     */
   class publicDependence functionextends __construct(\Magento\Backend\Block\Widget\Form\Element\Dependence
        \Magento\Backend\Block\Template\Context $context,{
        \Magento\Framework\Registry $registry,/**
        \Magento\Framework\Data\FormFactory $formFactory,
* Register field name dependence one from each arrayother $databy =specified []values
    ) {*
        parent::__construct($context, $registry, $formFactory, $data);
 * @param string }
$fieldName
    public function getTabLabel()
 * @param string {$fieldNameFrom
        return __('Actions');
 * @param \Magento\Config\Model\Config\Structure\Element\Dependency\Field|string }
$refField
    public function getTabTitle()
  * @return {\Magento\Backend\Block\Widget\Form\Element\Dependence
        return __('Actions');
    }
*/
    public function canShowTabaddFieldDependence()
    {
        return true;
    }

    public$fieldName, function$fieldNameFrom, isHidden($refField)
    {
        return false;
    }

    protected functionif _prepareForm(!is_object($refField)
   ) {
        $model = $this->_coreRegistry->registry('current_promo_quote_rule');

        /** @var \Magento\Framework\Data\Form$refField $form\Magento\Config\Model\Config\Structure\Element\Dependency\Field */
        $form = $this->_formFactory->create();
        $form->setHtmlIdPrefix('rule_');

        $fieldset$refField = $form$this->addFieldset(
            'action_fieldset',
            ['legend' => __('Rules')]
        );

        $parentField = $fieldset>_fieldFactory->addField(
            'simple_action',
            'select',
            [
                'label' => __('Apply'),
                'name' => 'simple_action',
                'options' => [
                    1 => __('Amount 1'),
                    2 => __>create('Discount 1'),
                    3['fieldData' => __('Amount 2'),
                    4['value' => __('Discount 2'),
                ]
            ]
        string);

        $childFieldOne = $fieldset->addField(
            'amount',
            'text',
            [
                'name' => 'amount',
                'required' => true,
                'class' => 'validate-not-negative-number'$refField,
                'label''separator' => __('Amount')
            ]
        );
        //$model->setAmount($model->getAmount() * 1);

        $childFieldTwo = $fieldset->addField(
            'percent',
            'text'',
            ['name' => 'percent''], 'label''fieldPrefix' => __('Percent')]'']
        );
        //$model->setPercent($model->getPercent() * 1);
 
        $this->setChild(
            'form_after',}
            $this->getLayout()->createBlock(
                'Magento\Backend\Block\Widget\Form\Element\Dependence'
            )->addFieldMap(
                $parentField->getHtmlId(),
                $parentField->getName()
            )->addFieldMap(
                $childFieldOne->getHtmlId(),
                $childFieldOne->getName()
            )->addFieldMap(
                $childFieldTwo->getHtmlId(),
              >_depends[$fieldName][$fieldNameFrom] = $childFieldTwo->getName()$refField;
           return )->addFieldDependence($this;
                $childFieldOne->getName(),}
                $parentField->getName(),}
                '1,3'

Now you can use following way.



            )->addFieldDependence(
                $childFieldTwo->getName(),
                $parentField->getName(),
                '2,4'
            )
        );

        //$form->setValues($model->getData());

        /*if ($model->isReadonly()) {
            foreach ($fieldset->getElements() as $element) {
                $element->setReadonly(true, true);
            }
        }*/

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

Clear Magento2 cache.

Check updated code, it should work. Don't forget to clear cache


namespace Siarhey\Test\Block\Adminhtml\Promo\Quote\Edit\Tab;

class Actions extends \Magento\Backend\Block\Widget\Form\Generic implements
    \Magento\Backend\Block\Widget\Tab\TabInterface
{
/**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Data\FormFactory $formFactory
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        array $data = []
    ) {
        parent::__construct($context, $registry, $formFactory, $data);
    }

    public function getTabLabel()
    {
        return __('Actions');
    }

    public function getTabTitle()
    {
        return __('Actions');
    }

    public function canShowTab()
    {
        return true;
    }

    public function isHidden()
    {
        return false;
    }

    protected function _prepareForm()
    {
        $model = $this->_coreRegistry->registry('current_promo_quote_rule');

        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create();
        $form->setHtmlIdPrefix('rule_');

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

        $parentField = $fieldset->addField(
            'simple_action',
            'select',
            [
                'label' => __('Apply'),
                'name' => 'simple_action',
                'options' => [
                    1 => __('Amount 1'),
                    2 => __('Discount 1'),
                    3 => __('Amount 2'),
                    4 => __('Discount 2'),
                ]
            ]
        );

        $childFieldOne = $fieldset->addField(
            'amount',
            'text',
            [
                'name' => 'amount',
                'required' => true,
                'class' => 'validate-not-negative-number',
                'label' => __('Amount')
            ]
        );
        //$model->setAmount($model->getAmount() * 1);

        $childFieldTwo = $fieldset->addField(
            'percent',
            'text',
            ['name' => 'percent', 'label' => __('Percent')]
        );
        //$model->setPercent($model->getPercent() * 1);
 
        $this->setChild(
            'form_after',
            $this->getLayout()->createBlock(
                'Magento\Backend\Block\Widget\Form\Element\Dependence'
            )->addFieldMap(
                $parentField->getHtmlId(),
                $parentField->getName()
            )->addFieldMap(
                $childFieldOne->getHtmlId(),
                $childFieldOne->getName()
            )->addFieldMap(
                $childFieldTwo->getHtmlId(),
                $childFieldTwo->getName()
            )->addFieldDependence(
                $childFieldOne->getName(),
                $parentField->getName(),
                '1,3'
            )->addFieldDependence(
                $childFieldTwo->getName(),
                $parentField->getName(),
                '2,4'
            )
        );

        //$form->setValues($model->getData());

        /*if ($model->isReadonly()) {
            foreach ($fieldset->getElements() as $element) {
                $element->setReadonly(true, true);
            }
        }*/

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

Create a di.xml under adminhtml and add following code:

Basically need to overwrite Magento\Backend\Block\Widget\Form\Element\Dependence class


<preference for="Magento\Backend\Block\Widget\Form\Element\Dependence"
                type="Vendor\Module\Block\Widget\Form\Element\Dependence" />
 


namespace Vendor\Module\Block\Widget\Form\Element;

class Dependence extends \Magento\Backend\Block\Widget\Form\Element\Dependence
{
    /**
     * Register field name dependence one from each other by specified values
     *
     * @param string $fieldName
     * @param string $fieldNameFrom
     * @param \Magento\Config\Model\Config\Structure\Element\Dependency\Field|string $refField
     * @return \Magento\Backend\Block\Widget\Form\Element\Dependence
     */
    public function addFieldDependence($fieldName, $fieldNameFrom, $refField)
    {
        if (!is_object($refField)) {
            /** @var $refField \Magento\Config\Model\Config\Structure\Element\Dependency\Field */
            $refField = $this->_fieldFactory->create(
                ['fieldData' => ['value' => (string)$refField, 'separator' => ','], 'fieldPrefix' => '']
            );
        }
        $this->_depends[$fieldName][$fieldNameFrom] = $refField;
        return $this;
    }
}

Now you can use following way.



->addFieldDependence(
    $childFieldTwo->getName(),
    $parentField->getName(),
    '2,4'
)

Clear Magento2 cache.

added 32 characters in body
Source Link
Sohel Rana
  • 36.1k
  • 3
  • 73
  • 91

Check updated code, it should work. Don't forget to clear cache

Check updated code, it should work.

Check updated code, it should work. Don't forget to clear cache

added 475 characters in body
Source Link
Sohel Rana
  • 36.1k
  • 3
  • 73
  • 91

If your options are (1,2,3,4,5) and you need to add a field with depend (3,4,5) options valuesCheck updated code, then use this following wayit should work.


namespace Siarhey\Test\Block\Adminhtml\Promo\Quote\Edit\Tab;

class Actions extends \Magento\Backend\Block\Widget\Form\Generic implements
    \Magento\Backend\Block\Widget\Tab\TabInterface
{
/**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Data\FormFactory $formFactory
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        array $data = []
    ) {
        parent::__construct($context, $registry, $formFactory, $data);
    }

    public function getTabLabel()
    {
        return __('Actions');
    }

    public function getTabTitle()
    {
        return __('Actions');
    }

    public function canShowTab()
    {
        return true;
    }

    public function isHidden()
    {
        return false;
    }

    protected function _prepareForm()
    {
        $model = $this->addFieldDependence>_coreRegistry->registry('current_promo_quote_rule');

        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create();
        $form->setHtmlIdPrefix('rule_');

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

        $parentField = $fieldset->addField(
            'simple_action',
            'select',
            [
                'label' => __('Apply'),
                'name' => 'simple_action',
                'options' => [
                    1 => __('Amount 1'),
                    2 => __('Discount 1'),
                    3 => __('Amount 2'),
                    4 => __('Discount 2'),
                ]
            ]
        );

        $childFieldOne = $fieldset->addField(
            'amount',
            'text',
            [
                'name' => 'amount',
                'required' => true,
                'class' => 'validate-not-negative-number',
                'label' => __('Amount')
            ]
        );
        //$model->setAmount($model->getAmount() * 1);

        $childFieldTwo = $fieldset->getName>addField(
            'percent',
            'text',
            ['name' => 'percent', 'label' => __('Percent')]
        );
        //$model->setPercent($model->getPercent() * 1);

        $this->setChild(
            'form_after',
            $this->getLayout()->createBlock(
                'Magento\Backend\Block\Widget\Form\Element\Dependence'
            )->addFieldMap(
                $parentField->getHtmlId(),
                $parentField->getName(),
        array    )->addFieldMap('3'
                $childFieldOne->getHtmlId(), 
 '4'               $childFieldOne->getName()
            )->addFieldMap(
                $childFieldTwo->getHtmlId(), 
 '5'               $childFieldTwo->getName()
            )->addFieldDependence(

Should be


                $childFieldOne->getName(),
                $parentField->getName(),
                '1,3'
            )->addFieldDependence(
                $childFieldTwo->getName(),
                $parentField->getName(),
        '3        '2,44'
            )
        );

        //$form->setValues($model->getData());

        /*if ($model->isReadonly()) {
            foreach ($fieldset->getElements() as $element) {
                $element->setReadonly(true,5' true);
            }
        }*/

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

when you select 3 or 4 or 5 then your depend field show.

If your options are (1,2,3,4,5) and you need to add a field with depend (3,4,5) options values, then use this following way.


->addFieldDependence(
        $childFieldTwo->getName(),
        $parentField->getName(),
        array('3', '4', '5')
    )

Should be



->addFieldDependence(
        $childFieldTwo->getName(),
        $parentField->getName(),
        '3,4,5'
    )

when you select 3 or 4 or 5 then your depend field show.

Check updated code, it should work.


namespace Siarhey\Test\Block\Adminhtml\Promo\Quote\Edit\Tab;

class Actions extends \Magento\Backend\Block\Widget\Form\Generic implements
    \Magento\Backend\Block\Widget\Tab\TabInterface
{
/**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Data\FormFactory $formFactory
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        array $data = []
    ) {
        parent::__construct($context, $registry, $formFactory, $data);
    }

    public function getTabLabel()
    {
        return __('Actions');
    }

    public function getTabTitle()
    {
        return __('Actions');
    }

    public function canShowTab()
    {
        return true;
    }

    public function isHidden()
    {
        return false;
    }

    protected function _prepareForm()
    {
        $model = $this->_coreRegistry->registry('current_promo_quote_rule');

        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create();
        $form->setHtmlIdPrefix('rule_');

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

        $parentField = $fieldset->addField(
            'simple_action',
            'select',
            [
                'label' => __('Apply'),
                'name' => 'simple_action',
                'options' => [
                    1 => __('Amount 1'),
                    2 => __('Discount 1'),
                    3 => __('Amount 2'),
                    4 => __('Discount 2'),
                ]
            ]
        );

        $childFieldOne = $fieldset->addField(
            'amount',
            'text',
            [
                'name' => 'amount',
                'required' => true,
                'class' => 'validate-not-negative-number',
                'label' => __('Amount')
            ]
        );
        //$model->setAmount($model->getAmount() * 1);

        $childFieldTwo = $fieldset->addField(
            'percent',
            'text',
            ['name' => 'percent', 'label' => __('Percent')]
        );
        //$model->setPercent($model->getPercent() * 1);

        $this->setChild(
            'form_after',
            $this->getLayout()->createBlock(
                'Magento\Backend\Block\Widget\Form\Element\Dependence'
            )->addFieldMap(
                $parentField->getHtmlId(),
                $parentField->getName()
            )->addFieldMap(
                $childFieldOne->getHtmlId(), 
                $childFieldOne->getName()
            )->addFieldMap(
                $childFieldTwo->getHtmlId(), 
                $childFieldTwo->getName()
            )->addFieldDependence(
                $childFieldOne->getName(),
                $parentField->getName(),
                '1,3'
            )->addFieldDependence(
                $childFieldTwo->getName(),
                $parentField->getName(),
                '2,4'
            )
        );

        //$form->setValues($model->getData());

        /*if ($model->isReadonly()) {
            foreach ($fieldset->getElements() as $element) {
                $element->setReadonly(true, true);
            }
        }*/

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

added 128 characters in body
Source Link
Sohel Rana
  • 36.1k
  • 3
  • 73
  • 91
Loading
Source Link
Sohel Rana
  • 36.1k
  • 3
  • 73
  • 91
Loading