Magento2 Admin Form – Adding Dependency in Custom Field

adminformcustom-fieldmagento2

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?

Best Answer

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
)
Related Topic