Check updated code, it should workCreate a di. Don't forget to clear cachexml under adminhtml and add following code:
Basically need to overwrite Magento\Backend\Block\Widget\Form\Element\Dependence class
namespace Siarhey\Test\Block\Adminhtml\Promo\Quote\Edit\Tab;
class Actions extends \Magento\Backend\Block\Widget\Form\Generic<preference implementsfor="Magento\Backend\Block\Widget\Form\Element\Dependence"
\Magento\Backend\Block\Widget\Tab\TabInterface
{
/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\Registrytype="Vendor\Module\Block\Widget\Form\Element\Dependence" $registry/>
* @param \Magento\Framework\Data\FormFactory $formFactory
* @param arraynamespace $dataVendor\Module\Block\Widget\Form\Element;
*/
class publicDependence functionextends __construct(\Magento\Backend\Block\Widget\Form\Element\Dependence
\Magento\Backend\Block\Template\Context $context,{
\Magento\Framework\Registry $registry,/**
\Magento\Framework\Data\FormFactory $formFactory,
* Register field name dependence one from each arrayother $databy =specified []values
) {*
parent::__construct($context, $registry, $formFactory, $data);
* @param string }
$fieldName
public function getTabLabel()
* @param string {$fieldNameFrom
return __('Actions');
* @param \Magento\Config\Model\Config\Structure\Element\Dependency\Field|string }
$refField
public function getTabTitle()
* @return {\Magento\Backend\Block\Widget\Form\Element\Dependence
return __('Actions');
}
*/
public function canShowTabaddFieldDependence()
{
return true;
}
public$fieldName, function$fieldNameFrom, isHidden($refField)
{
return false;
}
protected functionif _prepareForm(!is_object($refField)
) {
$model = $this->_coreRegistry->registry('current_promo_quote_rule');
/** @var \Magento\Framework\Data\Form$refField $form\Magento\Config\Model\Config\Structure\Element\Dependency\Field */
$form = $this->_formFactory->create();
$form->setHtmlIdPrefix('rule_');
$fieldset$refField = $form$this->addFieldset(
'action_fieldset',
['legend' => __('Rules')]
);
$parentField = $fieldset>_fieldFactory->addField(
'simple_action',
'select',
[
'label' => __('Apply'),
'name' => 'simple_action',
'options' => [
1 => __('Amount 1'),
2 => __>create('Discount 1'),
3['fieldData' => __('Amount 2'),
4['value' => __('Discount 2'),
]
]
string);
$childFieldOne = $fieldset->addField(
'amount',
'text',
[
'name' => 'amount',
'required' => true,
'class' => 'validate-not-negative-number'$refField,
'label''separator' => __('Amount')
]
);
//$model->setAmount($model->getAmount() * 1);
$childFieldTwo = $fieldset->addField(
'percent',
'text'',
['name' => 'percent''], 'label''fieldPrefix' => __('Percent')]'']
);
//$model->setPercent($model->getPercent() * 1);
$this->setChild(
'form_after',}
$this->getLayout()->createBlock(
'Magento\Backend\Block\Widget\Form\Element\Dependence'
)->addFieldMap(
$parentField->getHtmlId(),
$parentField->getName()
)->addFieldMap(
$childFieldOne->getHtmlId(),
$childFieldOne->getName()
)->addFieldMap(
$childFieldTwo->getHtmlId(),
>_depends[$fieldName][$fieldNameFrom] = $childFieldTwo->getName()$refField;
return )->addFieldDependence($this;
$childFieldOne->getName(),}
$parentField->getName(),}
'1,3'
Now you can use following way.
)->addFieldDependence(
$childFieldTwo->getName(),
$parentField->getName(),
'2,4'
)
);
//$form->setValues($model->getData());
/*if ($model->isReadonly()) {
foreach ($fieldset->getElements() as $element) {
$element->setReadonly(true, true);
}
}*/
$this->setForm($form);
return parent::_prepareForm();
}
}
Clear Magento2 cache.