4

How to set dependency of admin form field in magento 2?

How can i set dependency of admin fields

I am trying this code

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

    );

but its not working

EDIT

protected function _prepareForm()
{
    ......
    $model = $this->_coreRegistry->registry('answers');
    $form = $this->_formFactory->create();
    $form->setHtmlIdPrefix('page_');

     $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Answer Information')]);
    if ($model->getId()) {
        $fieldset->addField('answers_id', 'hidden', ['name' => 'answers_id']);
    }

    $fieldset->addField(
        'answers',
        'select',
        [
            'label' => __('Answer'),
            'title' => __('Answer'),
            'name' => 'answers',
            options => $this->_answerby->getOptionArray(),
        ]

    ); 
    $fieldset->addField(
        'is_status',
        'text',
        [
           'label' => __('Status'),
            'title' => __('Status'),
            'name' => 'is_status',
        ]

    ); 

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

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

    $this->setForm($form);

    return parent::_prepareForm();
}

 

5
  • Is it in system.xml ?
    – Niranjan B
    Commented Dec 31, 2015 at 14:25
  • if it is in system.xml then you an check following link magento.stackexchange.com/questions/95361/… Commented Jan 1, 2016 at 4:19
  • @NiranjanB and @ Bhupendra Jadeja No its not in system.xml its in admin form field
    – ND17
    Commented Jan 1, 2016 at 4:39
  • can you add your _prepareForm() function?
    – Newbie
    Commented Jan 1, 2016 at 7:28
  • @Newbie See my update
    – ND17
    Commented Jan 1, 2016 at 7:32

1 Answer 1

12

You should add this in your form.

protected function _prepareForm()
{
    ......
    $model = $this->_coreRegistry->registry('some_registry');
    $form = $this->_formFactory->create();
    $form->setHtmlIdPrefix('page_');
    $htmlIdPrefix = $form->getHtmlIdPrefix();
    $fieldset->addField(
        'answers',
        'select',
        [
            .....
        ],
        'to'
    ); 
    $fieldset->addField(
        'is_status',
        'text',
        [
           .....
            'display' => 'none'
        ],
        'answers'
    ); 
    // write this before  this line $this->setForm($form);
    $this->setChild(
        'form_after',
        $this->getLayout()->createBlock(
            'Magento\Backend\Block\Widget\Form\Element\Dependence'
        )->addFieldMap(
            "{$htmlIdPrefix}answers",
            'answers'
        )
        ->addFieldMap(
            "{$htmlIdPrefix}is_status",
            'is_status'
        )
        ->addFieldDependence(
            'is_status',
            'answers',
            'Customer'
        )
    );
    .....
}

This code works.

0

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.