Boolean checkbox is always in changed data, even if it didn't change.

Boolean checkbox is always in changed data, even if it didn't change.

annekateannekate Posts: 7Questions: 3Answers: 0

I have a field on my editor that is configured as a boolean checkbox like this, based on the example at the bottom of these docs:

{
  'data': 'has_cats',
  'name': 'has_cats',
  'label': 'Does the house have cats?',
  'type': 'checkbox',
  'options': [{'label': '', 'value': 1}],
  'unselectedValue': 0,
}

I also have my editor set up to only submit the changed data on submit like this in the editor options based on the docs here:

formOptions: {
  main: {
    submit: 'changed'
  },
}

This is working beautifully for all field types except the boolean checkbox, which is added to the changed data even if I didn't change it. I assume this has to do with the values changing slightly via the unselectedValue option.

If the value was already True, it doesn't add it to the changed fields, but if the value was False, and I edit the form (or even if I open the form and don't change anything), it picks up that field as "changed" in the data:

{
  'data[item-1][has_cats][]': ['0'],   // note the string of '0' instead of the integer
  'action': ['edit']
}

Is there a way to configure these fields to properly only show in the changed data if it was actually changed?

Answers

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    See if this example helps.

    Kevin

  • annekateannekate Posts: 7Questions: 3Answers: 0

    Thanks, Kevin! That is not quite what I'm looking for since we are using the bubble editor and not inline editing so we don't want the checkbox to show on the table itself. That also doesn't account for the fact that the post request payload is sending a string of '0' instead of the actual 0 value.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Hi,

    The issue is that we get the value from the checkbox's value parameter, which is always read back as a string. Typing information, unfortunately, is lost when using DOM attributes.

    One way to address this is with the fields.getFormatter option - you could "cast" the value back to an integer there - for example:

    getFormatter: vals => vals.map(v => v*1)
    

    or without arrow functions:

    getFormatter: function (vals) {
      return vals.map( function (v) {
        return v * 1;
      } );
    }
    

    That said, it is odd that unselectedValue is coming back as a string. It doesn't have a checkbox, and looking at the code, it does do any type conversion, so it should be a number.

    If the formatting function doesn't help (it should :)), can you link to a page showing the issue please?

    Allan

  • annekateannekate Posts: 7Questions: 3Answers: 0

    Thanks Allan! I'll need to look into that a bit because we have a slightly unique set up with our field configuration, which we are defining in a python file, converting to JSON, and passing to the JS where we configure all the datatables stuff (this works best for our setup using python+Django).

    Because of that setup, it's difficult to pass & parse javascript functions through that flow, but we're looking into maybe making a special case and defining this one field directly in the JS instead.

Sign In or Register to comment.