1

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(
        '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);
1

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(
        'Magento\Backend\Block\Widget\Form\Element\Dependence')
        ->addFieldMap($selectField->getHtmlId(), $selectField->getName())
        ->addFieldMap($hideField->getHtmlId(), $hideField->getName())
        ->addFieldMap($merchantsMail->getHtmlId(), $merchantsMail->getName())
        ->addFieldDependence($merchantsMail->getName(),$selectField->getName(),2)
        ->addFieldDependence($hideField->getName(),$selectField->getName(),1)
    );
    $fieldset->addField('send-comments', 'textarea', ['name' => 'send-comments', "readonly" => "true", 'label' => __('Enter Comments')]);
0

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' => '']
    );

   .......
    ->addFieldDependence
     (
       $field_to_show->getName(),
       $field_to_evaluate->getName(),
       $refField
      )
  • @Hits, This is the code to show the fields for value 1 and 2 both, not separate fields. – Saravanan DS Oct 17 '18 at 9:48

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.