How to Allow Null Value in Multiselect System Configuration in Magento 2

allow null value in multiselect system configuration in Magento 2

Ever used multiselect box in Magento 2 system configuration to avail the admin multi selection ability for some setting?

There are many circumstances when the merchants do not wish to select the value and want to have a null value in the multi select box in system configuration. For instance, you have added a day selector named “Non-Deliverable Days” to select the days of the week when the delivery is unavailable. But, during the holiday season, the admin wishes to deliver products on all the days of the week and thus requires to unselect all the values from the days selector.

By default, multiselect box works fine with saving multiselect values in the database but when the admin unselects all the selected multiselect values, it saves recently saved options from the database instead of clearing the options from the database. Thus, the admin can never allow null value in multiselect system configuration in Magento 2.

Before adding can be empty tag in multiselect box

Follow the solution below to allow null value in multiselect system configuration in Magento 2.

Solution to Allow Null Value in Multiselect System Configuration in Magento 2

  1. For example, you have created a system.xml file at app/code/Vendor/Module/etc/adminhtml to add a multiselect box in system configuration.
  2. Magento 2 has the below property to allow null value in multiselect. Add the <can_be_empty> tag in the code to serve your purpose.

After adding can be empty tag in multiselect box

Behind the scenes, when you set can_be_empty to 1 (true), the system renders a hidden field on the System Configuration page.

If you have queries regarding this blog, feel free to ask in the Comment section below.

I would be happy to answer.

Please do consider sharing this post with Magento Community via social media.

Thank you.

Get Weekly Updates

Never miss Magento tips, tricks, tutorials, and news.

Thank you for subscribing.

Something went wrong.

5
(based on 8 Reviews)

Kanchi is a Magento Developer at Meetanshi. Apart from that, she is into dancing. In her free time, she likes to make handmade art and craft.

Leave a reply:

Your email address will not be published.