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();
}

 

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

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.

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.