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.
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(
'Magento\Backend\Block\Widget\Form\Element\Dependence')
->addFieldMap($selectField->getHtmlId(), $selectField->getName())
->addFieldMap($hideField->getHtmlId(), $hideField->getName())
->addFieldDependence($hideField->getName(),$selectField->getName(),1);
$mailDependence = $this->getLayout()->createBlock(
'Magento\Backend\Block\Widget\Form\Element\Dependence')
->addFieldMap($selectField->getHtmlId(), $selectField->getName())
->addFieldMap($merchantsMail->getHtmlId(), $merchantsMail->getName())
->addFieldDependence($merchantsMail->getName(),$selectField->getName(),2);
$this->setChild('form_after', $dependence);
$this->setChild('form_after', $mailDependence);