0

I have a custom module in admin with it's own admin page controller and ui-form with a variety of fields.

One of these fields I want to be a 'checkboxset'. When I set the formElement value to that it shows properly as a list of checkboxes and I can select these and save these with some controller code that is saving the rest of the form fields (some input's and multiselects).

However, when the admin page reloads it does not show the saved values.

All the checkboxes start as unchecked.

I checked and the data is being saved in the database. And I can see in the page source that previously saved values are in the data_source.

I also tried switching it from a 'checkboxset' to a 'multiselect' and it worked without issue.

The code for the field is below:

<field name="condition_checklist" formElement="checkboxset" sortOrder="140">
  <argument name="data" xsi:type="array">
    <item name="options" xsi:type="object">Ose\Tradein\Model\Source\ConditionChecklist</item>
    <item name="config" xsi:type="array">
      <item name="source" xsi:type="string">Tradein</item>
      <item name="additionalInfo" xsi:type="string">You must tick all of the boxes</item>
    </item>
  </argument>
  <settings>
    <dataType>text</dataType>
    <label translate="true">Condition Checklist</label>
    <dataScope>condition_checklist</dataScope>
    <validation>
      <rule name="required-entry" xsi:type="boolean">false</rule>
    </validation>
  </settings>
</field>

I did a search of core magento to see if I could find an example of this field type being used but the only references to 'checkboxset' I could find were in test or when it's declared in magento-ui

2
  • How are you storing the selected values in the database? In what format? Feb 26, 2021 at 6:26
  • the values are stored in a comma seperated id format (eg. 1,6,8) in a text type column. For the multiselect fields this works.
    – Typhado
    Feb 28, 2021 at 22:31

1 Answer 1

0

When you save the form, convert the array to json, so it can be saved as a string. This is an example from the controller I use to save the form:

if (isset($data['condition_checklist'])) {
    $data['condition_checklist'] = json_encode($data['condition_checklist']);
}

Then in the DataProvider, convert the json back to an array.

$this->loadedData[$model->getId()] = $model->getData();
foreach ($items as $model) {
    if (isset($this->loadedData[$model->getId()]['condition_checklist'])) {
        $this->loadedData[$model->getId()]['condition_checklist'] = json_decode($this->loadedData[$model->getId()]['condition_checklist']);
    }
}

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.