3

I have a form in admin with dropdown and textbox.

I want to show textbox only if dropdown value is set to yes.

I have not used UI-component for this.

I have tried below code but its not working.

<?php
namespace Company\Module\Block\Adminhtml\Syncpolicy\Edit\Tab;
class Listrules extends \Magento\Backend\Block\Widget\Form\Generic implements \Magento\Backend\Block\Widget\Tab\TabInterface
{
    /**
     * @var \Magento\Store\Model\System\Store
     */
    protected $_systemStore;

    protected $_resources;

    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param \Magento\Framework\Data\FormFactory $formFactory
     * @param \Magento\Store\Model\System\Store $systemStore
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        \Magento\Store\Model\System\Store $systemStore,
        array $data = array()
    ) {
        $this->_systemStore = $systemStore;
        parent::__construct($context, $registry, $formFactory, $data);
    }

    /**
     * Prepare form
     *
     * @return $this
     */
    protected function _prepareForm()
    {
        /* @var $model \Magento\Cms\Model\Page */
        $model = $this->_coreRegistry->registry('module_syncpolicy_format');
            $isElementDisabled = false;
        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create();
        $form->setHtmlIdPrefix('page_');

        $htmlIdPrefix = $form->getHtmlIdPrefix();

        $fieldset = $form->addFieldset('base_fieldset', array('legend' => __('Synchronization Policy')));
        if ($model->getId()) {
            $fieldset->addField('id', 'hidden', array('name' => 'id'));
        }


        $selectField = $fieldset->addField('select_field', 'select', array(
                'label' => 'Show/Hide Selection',
                'name' => 'select_field',           
                'values' => array(
                    array(
                        'value' => 1,
                        'label' => 'Yes',
                    ),
                    array(
                        'value' => 0,
                        'label' => 'No',
                    ),
                ),
                'onChange'  => 'showHideField()',
            ));


        $fieldset->addField('field_to_hide', text, array(
            'label'  => $this->__('Test'),
        ));


        $selectField->setAfterElementHtml('
                                <script>
                                function showHideField() {                                              
                                    $("field_to_hide").toggle()
                                }
                                </script>
                            ');


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

        return parent::_prepareForm();   
    }

    /**
     * Prepare label for tab
     *
     * @return string
     */
    public function getTabLabel()
    {
        return __('List Rules');
    }

    /**
     * Prepare title for tab
     *
     * @return string
     */
    public function getTabTitle()
    {
        return __('List Rules');
    }

    /**
     * {@inheritdoc}
     */
    public function canShowTab()
    {
        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function isHidden()
    {
        return false;
    }

    /**
     * Check permission for passed action
     *
     * @param string $resourceId
     * @return bool
     */
    protected function _isAllowedAction($resourceId)
    {
        return $this->_authorization->isAllowed($resourceId);
    }
}
3
  • Can you show whole form class here?
    – Sohel Rana
    Jan 20, 2017 at 6:46
  • Hi Sohel, I have edited my question and added whole form class.
    – Nitz
    Jan 20, 2017 at 6:58
  • @Nitz Have you find any solution
    – Pawankumar
    Jul 10, 2019 at 14:50

1 Answer 1

6

For field dependency in admin form, try below code

$selectField = $fieldset->addField('select_field', 'select', array(
    'label' => 'Show/Hide Selection',
    'name' => 'select_field',           
    'values' => array(
        array(
            'value' => 1,
            'label' => 'Yes',
        ),
        array(
            'value' => 0,
            'label' => 'No',
        ),
    )
));
$hideField = $fieldset->addField('field_to_hide', 'text', ['name' => 'field_name','value'=> 'your_value', 'label' => __('Your label Name')]
));
//code to dependence field
$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'
    );
$this->setChild('form_after', $dependence);
3

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.