formBuilder: Required checkbox group does not render checked values

Description:

A checkbox group that is required does not render saved checked values. The checkboxes render correctly if the checkbox group is not required.

Environment Details:

  • formBuilder Version: 3.1.3
  • Browser: Chrome
  • OS: Win 10

Expected Behavior

The checkboxes should display as checked if the checkbox is present in the userData array.

Actual Behavior

The checkboxes do not show as checked for required checkbox groups.

Steps to Reproduce

var formData = [ { "type": "checkbox-group", "required": true, "label": "Checkbox Group", "name": "checkbox-group-1550096191629", "values": [{ "label": "Option 1", "value": "option-1" }, { "label": "Option 2", "value": "option-2" }], "userData": ["option-1"] }]; var formRenderOpts = { formData }; $('#fb-viewer').formRender(formRenderOpts);

I have implemented the following work-around, but I want to make sure I am not missing a configuration item somewhere.

var data = formData; if (typeof data !== 'object') { data = JSON.parse(data); } $.each(data, function (i, item) { if (item.type === "checkbox-group") { if (typeof item.required !== 'undefined' && item.required === true) { if (item.userData.length > 0) { $('.field-' + item.name).find('input').each(function (i, e) { if ($.inArray(e.value, item.userData) !== -1) { $(e).prop('checked', true); } }); } } } });

Screenshot - (optional)

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 6
  • Comments: 15 (2 by maintainers)

Commits related to this issue

Most upvoted comments

This issue is pretty annoying. As far as I can tell this is caused by the following:

    if (type === 'checkbox-group' && data.required) {
      this.onRender = this.groupRequired
    }

on Line 35-37 of select.js

This replaces the onRender function later on in select.js which is responsible for setting the userData of the checkbox-group. I don’t understand what groupRequired is doing, but it never seems to even consider the userData.