3

I am trying to add field dependency on magento2. i follow this thread but its not working for me. I am not sure if i miss something.

Here is code which i have implement.

use Magento\Backend\Block\Widget\Form\Generic;
use Magento\Backend\Block\Widget\Tab\TabInterface;



class Main extends Generic implements TabInterface
{

    /**
     * {@inheritdoc}
     */
    public function getTabLabel()
    {
        return __('Banner Information');
    }

    /**
     * {@inheritdoc}
     */
    public function getTabTitle()
    {
        return __('Banner Information');
    }

    /**
     * {@inheritdoc}
     */
    public function canShowTab()
    {
        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function isHidden()
    {
        return false;
    }

    /**
     * Prepare form before rendering HTML
     *
     * @return $this
     * @SuppressWarnings(PHPMD.NPathComplexity)
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    protected function _prepareForm()
    {
        $model = $this->_coreRegistry->registry('current_kensium_categorybanner_items');
        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create();
        $form->setHtmlIdPrefix('item_');
        $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Item Information')]);

        $htmlIdPrefix = $form->getHtmlIdPrefix();
        $fieldset->addField(
            'answers',
            'select',
            [
                'name' => 'answers',
                'label' => __('Banner Type 22'),
                'title' => __('Banner Type 22'),
                'required' => false,
                'options' => ['1' => __('Customer'), '0' => __('PDP')]
            ]

        );

        $fieldset->addField(
            'is_status',
            'text',
            [
                'name' => 'is_status',
                'label' => __('Category 22'),
                'title' => __('Category 22')

            ],
            'answers'
        );

        if ($model->getId()) {
            $fieldset->addField('id', 'hidden', ['name' => 'id']);
        }

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

        $this->setChild(
            'form_after',
            $this->getLayout()->createBlock(
                'Magento\Backend\Block\Widget\Form\Element\Dependence'
            )->addFieldMap(
                "item_answers",
                'answers'
            )
                ->addFieldMap(
                    "item_is_status",
                    'is_status'
                )
                ->addFieldDependence(
                    'is_status',
                    'answers',
                    'Customer'
                )
        );

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

Does anyone have implemented it?

  • Have you check consol and log file? Is that any error? – Dhiren Vasoya Nov 16 '17 at 10:49
  • @DhirenVasoya Yes. I didn't see any error in log file as well as console. dependent field is hidden but its not getting displayed while select option. – Kul Nov 16 '17 at 10:51
  • try passing 1 instead of Customer in addFieldDependence – Piyush Nov 16 '17 at 10:51
  • 1
    @Piyush great man! i have not thought about it silly mistake. please add as answer so i can accept. – Kul Nov 16 '17 at 10:56
1

You have to change your below code, change Customer to 1

->addFieldDependence(
     'is_status',
    'answers',
    '1' // here you have to pass 1 which is the value of your field option
)

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.