1

I've created a custom module in which I've a drop down field having "Yes/No" value, here is the screenshot:

https://nimb.ws/bMZCUB

I just want to replace this drop down into switch ,just like below screenshot:

https://nimb.ws/OCldgW

How can I achieve the same?

I've created module from "Silksoftware" and to creating the form fields it's using block just like:

   $fieldset->addField(
            'available_every_week',
            'select',
            [
                'label' => __('Available Every Week'),
                'title' => __('Available Every Week'),
                'name' => 'available_every_week',

                'options' => \Namespace\Test\Block\Adminhtml\Gametype\Grid::getOptionArray5(),
                'disabled' => $isElementDisabled
            ]
        );

Where

Namespace\Test\Block\Adminhtml\Gametype\Grid::getOptionArray5

is returning values(Yes/No) for the dropdown.

Waiting for the update.

  • thanks for the reply,this doesn't work getting " Class boolean does not exist" problem. nimb.ws/DEUX8o – wakar Ahamad Feb 15 '19 at 13:06
0

You need to create your form using UI component library provided in magento 2.

Use below example:

<field name="available_every_week" formElement="checkbox">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="source" xsi:type="string">category</item>
            <item name="default" xsi:type="number">1</item>
        </item>
    </argument>
    <settings>
        <validation>
            <rule name="required-entry" xsi:type="boolean">false</rule>
        </validation>
        <dataType>boolean</dataType>
        <label translate="true">Available Every Week</label>
    </settings>
    <formElements>
        <checkbox>
            <settings>
                <valueMap>
                    <map name="false" xsi:type="string">0</map>
                    <map name="true" xsi:type="string">1</map>
                </valueMap>
                <prefer>toggle</prefer>
            </settings>
        </checkbox>
    </formElements>
</field>
| improve this answer | |
  • thanks for the reply! I've created from Silksoftware,which is creating form just like Magento 1 way.is this possible to add switch in this? – wakar Ahamad Feb 15 '19 at 13:08
  • No, it is not possible without UI component form. – Nikunj Rathod Feb 15 '19 at 13:12

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.