Repository: django-helpdesk/django-helpdesk

Issue:
TicketCustomFieldValue Boolean (if not required) does not set in EditTicketForm
**Describe the bug**
When a Custom Field Value is created as Boolean and is Not Required the value never sets. If Required the value is only allowed to be set to true.

django-helpdesk        0.3.0b3
Django                 3.1.5

**To Reproduce**
Create a Custom Field as Boolean and Not Required.
Try setting the value and saving a ticket. When you are in the Ticket edit screen it is always checked as True by default, but when you save and view the ticket it is always False.
Set the Custom Field as Required and repeat the above steps and the system forces you to set the boolean as true, it cannot be set to false.

I did not look into the code to determine where the problem is, so if someone is already familiar and can make the change, much appreciated.


Discussion:
Required on boolean is expected behavior in Django, focusing on when Required is not set now.

https://docs.djangoproject.com/en/3.1/ref/forms/fields/#booleanfield

`Since all Field subclasses have required=True by default, the validation condition here is important. If you want to include a boolean in your form that can be either True or False (e.g. a checked or unchecked checkbox), you must remember to pass in required=False when creating the BooleanField.`


For some reason the CheckboxInput widget is not displaying or saving the value, forms.py. I tried NullBooleanSelect and this one saves a newly selected value properly, but when opening the edit form does not display the current value.
For the BooleanField I found the following:
`initial = True` renders an HTML input that uses True as the POST value if the checkbox is checked. If initial is False the POST value for a check in the checkbox is False and thus checked or unchecked the POST sends a value that translates to False in Python.
`required = True` renders HTML that requires the checkbox to be checked.

In HTML the input tag uses the `checked` key to check the checkbox, if `checked` is removed the box is unchecked. I haven't figured out how to change that using the BooleanField or CheckboxInput Widget yet.
`<input type="checkbox" name="custom_whatever" class="form-control" id="id_custom_whatever" checked="">`

From Django 3.1 documentation:
The HTML generated by the built-in widgets uses HTML5 syntax, targeting <!DOCTYPE html>. For example, it uses boolean attributes such as checked rather than the XHTML style of checked='checked'.

Anyone know how this works? I'm spending a lot of time trying to figure this out, figure someone knows...
I think I could give it a try 😉

Resolving patch:
diff --git a/helpdesk/forms.py b/helpdesk/forms.py
--- a/helpdesk/forms.py
+++ b/helpdesk/forms.py
@@ -98,6 +98,9 @@ def __init__(self, *args, **kwargs):
             try:
                 current_value = TicketCustomFieldValue.objects.get(ticket=self.instance, field=field)
                 initial_value = current_value.value
+                # If it is boolean field, transform the value to a real boolean instead of a string
+                if current_value.field.data_type == 'boolean':
+                    initial_value = initial_value == 'True'
             except TicketCustomFieldValue.DoesNotExist:
                 initial_value = None
             instanceargs = {


Test patch: