0

my code is in below here how can i set 'gateway_title' and 'gateway_title2' this tow fields depends on 'gateway_is_active' filed both of those two filed have be depended.

$fieldset->addField(
        'gateway_is_active', 'select',
        array(
            'label'  => 'Enable Email Gateway',
            'name'   => 'gateway[is_active]',
            'values' => $options,
            'note'   => 'Converting new emails that come to a gateway mailbox to tickets'
        )
   );
   $fieldset->addField(
        'gateway_title', 'text',
        array(
            'label'    => 'Title',
            'name'     => 'gateway[title]',
            'required' => true,
        )
    );

    $fieldset->addField(
        'gateway_title2', 'text',
        array(
            'label'    => 'Title',
            'name'     => 'gateway[title2]',
            'required' => true,
        )
    );
0

After successfully created basic form .Now you can add following code into Form.php

$action = $fieldset->addField(
    "main_field",
    "select",
    [
        "label"     =>      __("Main Field"),
        "class"     =>      "required-entry",
        "name"      =>      "main_field",
        "values"    =>      [
            ["value" => 0,"label" => __("Do Nothing")],
            ["value" => 1,"label" => __("Display Field")],
        ]
    ]
);

$anotherField = $fieldset->addField(
    "dependent_field",
    "text",
    [
        "label"     => __("Dependent Field"),
        "class"     => "required-entry",
        "required"  => true,
        "name"      => "dependent_field"
    ]
);

$this->setChild(
    'form_after',
    $this->getLayout()->createBlock('\Magento\Backend\Block\Widget\Form\Element\Dependence')
    ->addFieldMap($action->getHtmlId(), $action->getName())
    ->addFieldMap($anotherField->getHtmlId(), $anotherField->getName())
    ->addFieldDependence($anotherField->getName(), $action->getName(), 1)
);

In first section we create main field select the value of this field display dependent field.Second section create dependent field.In third we define which field display after select main field and also define the value.

You can check screen-shot.In first screen-shot initial state of form and second screen-shot show dependent field after select main field value.

enter image description here

enter image description here

I hope this blog will help you to create dependent field in your custom form.If you get any issue or query then you can comment below.Thanks

Source

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.