In Magento 2.2.5, I want to use the select and text fields. That fields should show based on the another select (dropdown) values.

ie., if value 1 means, should show select field, if the value 2 means, should show text field.

Magento 1 working ref.

Here is my code:

$selectField = $fieldset->addField('publish-type','select', [ 'name' => 'merchant-type', 'label' => __('Publish To'), 'title' => __('Publish To'), 'required' => false, 'options' => $selectType ]); $hideField = $fieldset->addField('merchant-type', 'select', ['name' => 'merchants-mail-id', 'label' => __('Enter Merchants Mail ids'), 'options' => $merchantgroupArray] ); $merchantsMail = $fieldset->addField('merchants-mail-id', 'textarea', ['name' => 'merchants-mail-id', "readonly" => "true", 'label' => __('Enter Merchants Mail ids')]);

$dependence = $this->getLayout()->createBlock(
        ->addFieldMap($selectField->getHtmlId(), $selectField->getName())
        ->addFieldMap($hideField->getHtmlId(), $hideField->getName())

    $mailDependence = $this->getLayout()->createBlock(
        ->addFieldMap($selectField->getHtmlId(), $selectField->getName())
        ->addFieldMap($merchantsMail->getHtmlId(), $merchantsMail->getName())
    $this->setChild('form_after', $dependence);
    $this->setChild('form_after', $mailDependence);

2 Answers 2


Actually the same M1 format is working. I missed form id and name.

$selectField = $fieldset->addField('send-to','select',['name' => '**send-to**','label' => __('Publish To'),'title' => __('Publish To'),'required' => true,'options' => $selectType]);
$hideField =  $fieldset->addField('merchant-type', 'select', ['name' => 'merchant-type', 'required' => true,'label' => __('Select Merchant Type'),'options' => $merchantgroupArray]);
$merchantsMail = $fieldset->addField('merchants-mail-id', 'textarea', ['name' => 'merchants-mail-id','required' => true, 'label' => __('Enter Merchants Mail ids')]
$this->setChild('form_after', $this->getLayout()->createBlock(
        ->addFieldMap($selectField->getHtmlId(), $selectField->getName())
        ->addFieldMap($hideField->getHtmlId(), $hideField->getName())
        ->addFieldMap($merchantsMail->getHtmlId(), $merchantsMail->getName())
    $fieldset->addField('send-comments', 'textarea', ['name' => 'send-comments', "readonly" => "true", 'label' => __('Enter Comments')]);

the same issue already reported in Magento issue

Maybe you will find your solution there.

Already srenon has mentioned how to pass multiple dependencies using addFieldDependence, please check there.

Check this.

Possible solution without editing core would be to create a DI for \Magento\Config\Model\Config\Structure\Element\Dependency\FieldFactory $_fieldFactory then

    $refField = $this->_fieldFactory->create(
         ['fieldData' => ['value' => 'dependecy 1,dependecy 2', 'separator' => ','], 'fieldPrefix' => '']

  • @Hits, This is the code to show the fields for value 1 and 2 both, not separate fields. Commented Oct 17, 2018 at 9:48

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.