monim67 / django-bootstrap-datepicker-plus

Bootstrap3/Bootstrap4/Bootstrap5 DatePickerInput, TimePickerInput, DateTimePickerInput, MonthPickerInput, YearPickerInput with date-range-picker functionality for django >= 2.0
https://pypi.python.org/pypi/django-bootstrap-datepicker-plus
MIT License
223 stars 61 forks source link

Clear/deleting does NOT clear form value #102

Open jjulian91 opened 1 year ago

jjulian91 commented 1 year ago

Describe the bug A clear and concise description of what the bug is.

To Reproduce Steps to reproduce the behavior:

  1. Go to any page with picker information that already exists
  2. Click on clear on widget or delete the value
  3. submit form
  4. Data NOT cleared

Expected behavior Clear button or deleting value from form to delete the value in the javascript.

Setup Information (please complete the following information):

[x] I have followed the configuration instructions and checked out the common error troubleshooting page.

christianwgd commented 1 year ago

I've tried to reproduce the problem in my minimal example (https://github.com/christianwgd/date_time_picker_sample). Everything is working as expected. Maybe you can compare the relevant code to find out what the difference is or extend the example to show the problem in detail.

jjulian91 commented 1 year ago

image

image

christianwgd commented 1 year ago

Not sure, what the images should tell us. Could you please add some comments? Thanks

jjulian91 commented 1 year ago

image

image

The first image is on page load, value in place.

Send image is the field cleared (using the clear button on the widget) yet the value is persisted in both the widget and the hidden input.

christianwgd commented 1 year ago

Yes, I think we all had understood, what your problem is. But we want to find out why. So, did you compare your code to that one in the sample? Can we see your code (view, form and template)? It's really hard to help you without any clue...

jjulian91 commented 1 year ago

Form: image

View: image

Inherited View: image

template: image

christianwgd commented 1 year ago

First of all: It would be much easier to read, if you would paste the code as text and not as images, thanks. If I want to test if your code is working, I would have to change my minimal example to work like your app. I need some time for that. Did you ever solve #98? If so, then it would be nice if you close that. If not, are your sure you're not failing from that?

A hint on your model form: As far as I know, the required property is only for simple forms (forms.Form). For model forms this is dependent on your model fields. These are optional in your form, if they have the "blank=True" property set. I'm not sure if you can override that with the "required" option.

jjulian91 commented 1 year ago

First of all: It would be much easier to read, if you would paste the code as text and not as images, thanks. If I want to test if your code is working, I would have to change my minimal example to work like your app. I need some time for that. Did you ever solve #98? If so, then it would be nice if you close that. If not, are your sure you're not failing from that?

A hint on your model form: As far as I know, the required property is only for simple forms (forms.Form). For model forms this is dependent on your model fields. These are optional in your form, if they have the "blank=True" property set. I'm not sure if you can override that with the "required" option.

No #98 is not solved -- no they are not related; the issue with #98 is the CSS doesn't account for viewport size of embedded templates.

It should be pretty clear that the form I sent is a model form that is directly tied to the model view/update view. The use of inline formsets here are pretty irrelevant and should be able to replicate with a simple view/form -- I only grabbed the exact example I dealt with when the page came up -- the issue seems to be that on page render of an initial value being added to the widget, the only time the value is touched is when it is "changed" to a new value, not changed to a null value.

model forms are able to be modified in way I have done it, as the super().init() loads the model definition, and the subsequent override of the required attr is then changed as if the model definition included blank=True -- this does not work for non nullable fields, but does work if the field is nullable as this field is.

git does Not like djangos template engine. -- so I won't be able to upload the template. And even the view/form give issues.

`

class invoiceTechnicianDetailList(UpdateView): template_name = 'invoices/technicians/technician_record.html' form_class = TechnicianDispatchRecordForm model = invoice.TechnicianDispatchRecord SUBFIELDLIST = ['note', 'changenote', 'finishnote', 'poMileage', 'start', 'finish'] attr = 'dispatch_record'

def get_success_url(self):
    return reverse_lazy('job_update', kwargs={'pk': self.object.invoice.pk})

def populateSelfVars(self, kwargs):
    self.invoice = invoice.Invoice.objects.get(pk=kwargs.get('invoice'))
    self.technician = technician.Technician.objects.get(
        pk=kwargs.get('technician'))

def form_valid(self, form):
    if form.is_valid():

            # remove signal here so that on back end adjustments, the signal doesn't fire
            signals.post_save.disconnect(
                receiver=dipatchSave, sender=invoice.TechnicianDispatchRecord)
            super().form_valid(form)
            # add back signal
            signals.post_save.connect(
                receiver=dipatchSave, sender=invoice.TechnicianDispatchRecord)

            with atomic():
            return HttpResponseRedirect(self.get_success_url())
    return super().form_invalid(form)

` for all intents and purposes -- all formset data can be stripped from the context data and will still provide the same error.

` class TechnicianDispatchRecordForm(ModelForm):

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    for field in self.fields.keys():
        self.fields[field].required = False

class Meta:
    model = invoice.TechnicianDispatchRecord
    exclude = ['dispatch_record', 'technician']

    widgets = {
        'dispatch_date': DatePickerInput(),
        'job_start_time': TimePickerInput(),
        'estimated_completion_time': TimePickerInput(),
        'drive_start': DateTimePickerInput(options={'showClear': True}),
        'drive_end': DateTimePickerInput(range_from='drive_start', options={'showClear': True}),
        'start': DateTimePickerInput(range_from="drive_end", options={'showClear': True}),
        'end': DateTimePickerInput(range_from="start", options={'showClear': True}),
    }

`

monim67 commented 7 months ago

I can't reproduce the problem on widget's demo website, @jjulian91 I'd suggest check the demo website to figure out what are you doing different if you still have the issue.