How to 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.
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
- For example, you have created a system.xml file at app/code/Vendor/Module/etc/adminhtml to add a multiselect box in system configuration.
1234567891011121314151617<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"><system><section id="extension_name"><group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1"showInStore="1"><field id="disable_delivery_date" translate="Disable Delivery Date" type="multiselect" sortOrder="8"showInDefault="1" showInWebsite="1"showInStore="1"><label>Non-Deliverable Days</label><source_model>Vendor\Module\Ui\Component\Listing\column\Multiselect</source_model></field></group></section></system></config> - 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.
1<can_be_empty>1</can_be_empty>
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.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Remove JS and CSS in Magento 2 Using Layout XML
How To Use Class in Root Script in Magento 2
Next