1

I am currently working on a Magento 2 extension. In my task, I need a field in admin grid to show a list of product's default attributes. My current solution is to use 'multiselect' to generate the UI. So in system.xml, the piece of code below has been added:

<field id="default" translate="label" type="multiselect" sortOrder="60" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Default Attributes </label>
    <source_model>...\DefaultAttributeSource</source_model>
</field>

By doing so, everything works fine. However, the requirement is to use checkboxes for this field. So, I have to change type="checkboxes" in the preceding code, this is where the problem came from.

After I click some of the checkboxes and press [Save Config] button, only the the last checkbox is checked, and only the one value is saved in the core_config_data table .

By looking at the Html source code, I guess the problem might be caused by the name attribute in <input type="checkbox" ...> element, it should be named as a array, like name="values[]". I also found when I check the checkboxes, Magento doesn't add checked="checked" to the element, instead, it adds a :after Pseudo class for it.

So, my question is how to save all the checked checkboxes to 'core_config_data' as a 'multiselect' does, such as:

| scope   | scope_id | path                | value              |
|---------|----------|---------------------|--------------------|
| default |    0     | attributes/default  | 128,111,97,100 ... |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.