3
I want Hide/show admin form field on toggle value using UiComponent.

when enable the toggle fields should be visible and on disable the toggle fields should not be visible.

Example:
[When enable the toggle][1]
[When Disable the toggle][2]
2
You can use below code for Hide/show admin form field and change field name accordingly.

app/code/Namespace/ModuleName/view/adminhtml/web/js/form/element/options.js

define([
    'underscore',
    'uiRegistry',
    'Magento_Ui/js/form/element/select',
    'Magento_Ui/js/modal/modal'
], function (_, uiRegistry, select, modal) {
    'use strict';

    return select.extend({

        /**
         * On value change handler.
         *
         * @param {String} value
         */
        onUpdate: function (value) {
            console.log('Selected Value: ' + value);

            var field1 = uiRegistry.get('index = test2');
            if (field1.visibleValue == value) {
                field1.show();
            } else {
                field1.hide();
            }

            var field2 = uiRegistry.get('index = test3');
            if (field2.visibleValue == value) {
                field2.show();
            } else {
                field2.hide();
            }

            return this._super();
        },
    });
});

Thanks!

|improve this answer|||||
  • Do you have any idea how to do same with text input, like if we open form in edit mode then only field should be visible? – Ronak Chauhan Jan 13 at 9:24
  • @RonakChauhan I don't have implement for text input. But you can try with get text input value and check, might that things you can help. – Kirti Nariya Jan 21 at 9:52
  • I have found the solution for text toggle. it can work without js @Kirti Nariya – Ronak Chauhan Jan 21 at 10:05
  • @RonakChauhan It is a nice. can you please share that solution as you get. so also it is helpful in future. – Kirti Nariya Jan 21 at 10:08

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.