Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear/deleting does NOT clear form value #102

Open
jjulian91 opened this issue Aug 3, 2023 · 9 comments
Open

Clear/deleting does NOT clear form value #102

jjulian91 opened this issue Aug 3, 2023 · 9 comments

Comments

@jjulian91
Copy link

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):

  • OS: Ubuntu 22.04
  • Browser chrome
  • Browser version 115.0.5790.110
  • Python version 3.10
  • Django version 4.2
  • Bootstrap version 4
  • jQuery version 3.6.1

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

@christianwgd
Copy link

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
Copy link
Author

image

image

@christianwgd
Copy link

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

@jjulian91
Copy link
Author

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
Copy link

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
Copy link
Author

Form:
image

View:
image

Inherited View:
image

template:
image

@christianwgd
Copy link

christianwgd commented Aug 4, 2023

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
Copy link
Author

jjulian91 commented Aug 4, 2023

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
Copy link
Owner

monim67 commented Feb 11, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants