Menu

#4782 Custom Fields with checkboxes can't be unchecked

open
nobody
None
2019-03-08
2019-03-08
Anonymous
No

Originally created by: gitsnow

Prerequisites

  • [ ] Can you reproduce the problem in a fresh installation of the "develop" branch?
  • [ ] Do you have any errors in the PHP error log, or javascript console?
  • [X] Did you check the osTicket forums?
  • [X] Did you perform a cursory search to see if your bug or enhancement is already reported?

For more information on how to write a good bug report

Description

Custom Fields with checkboxes can't be unchecked. This issue occurs because both posting and serializing forms don't account for unchecked inputs.

Steps to Reproduce

  1. Add Custom Field with type checkbox to any form
  2. Edit the form field and check the checkbox and Save.
  3. Edit the form field again, uncheck the field and try to Save. You will receive an error that the field "is already assigned this value". If the entire form instead of just the field is edited, the Save will complete successfully, but the field will still be checked.

Expected behavior: [What you expected to happen]
Field should be saved with the unchecked value

Actual behavior: [What actually happened]
Field is not updated and error pops up

Versions

osTicket 1.11.0
PHP 7.2.15
Ubuntu 18.04

Fix

I was able to create a workaround that checks unchecked checkboxes, but sets their values to 0. This works even though custom checkbox fields have their values set to their ID. I also hid the inputs before submission to avoid the user seeing all checkboxes as checked.

Edit scp/js/scp.js

Change Line 702 from

function(e) { submit_button = $(this); });

to

function(e) { submit_button = $(this); uncheckedPrep(); });

Add this to bottom of scp/js/scp.js

function uncheckedPrep() {
    $("form").submit(function () {
        var form = $(this);
        form.find('input[type="checkbox"]').each(function () {
            var currentCheckbox = $(this);
            currentCheckbox.hide();
            if (currentCheckbox.is(':checked') == false) {
                currentCheckbox.prop('checked', true);
                currentCheckbox.attr('value', '0');
            }
        });
    });
}
uncheckedPrep();

Discussion

  • Anonymous

    Anonymous - 2019-03-08

    Originally posted by: JediKev

    @gitsnow

    I’ll try to reproduce this tomorrow. You shouldn’t have to add a whole new function, it’s most likely a bug or maybe something that needs updating to the new jQuery standards.

    Cheers.

     
  • Anonymous

    Anonymous - 2019-03-08

    Originally posted by: gitsnow

    @JediKev

    I figured as much because this issue looks to have been fixed in past versions. I just provided my workaround until a more elegant solution could be put into place.

    Thanks for the quick reply!

     

Log in to post a comment.

MongoDB Logo MongoDB