Friday, October 8, 2021

Re: Problem re-rendering ClearableFileInput on validation error

Did you find a solution for this?

On Saturday, December 12, 2020 at 1:13:39 AM UTC+9 michael....@gmail.com wrote:
Note that if the user decides to upload a new file on step 2, the field will then be re-rendered with the uploaded file object, which also ends up rendering a blank file input. Is there a case where FileField.bound_data() should return anything else than the initial value?



On Fri, Dec 11, 2020 at 10:36 AM Michael Goffioul <michael....@gmail.com> wrote:
When a form containing a ClearableFileInput is re-rendered (e.g. due to validation error of some other field), and the clear checkbox was checked on the first submit, the widget is rendered without initial value, as empty file input.

For instance, let's says there's a model with ImageField and all the form/view mechanics to edit the model. You edit a model instance that has an uploaded image already:
1. on initial render, the field is rendered with the clear checkbox and link to existing file
2. submit the form with some invalid data (e.g. there's another field in the form that does not validate)
3. on re-render, the field is rendered as empty file input, without clear checkbox and link

AFAICS, this is because:
- ClearableFileInput.value_from_datadict() returns False
- FileField.bound_data() returns False: it only returns initial data if submitted data is None or FILE_INPUT_CONTRADICTION
- BoundField.value() returns False
- BoundField.as_widget() renders field with value set to False

I find it non-intuitive that the field is re-rendered as blank, as if there was no initial data. Is this the intended behavior? Or am I missing something?

Michael.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4133479d-8781-48d5-9e13-8080504a5749n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8e4e4169-44e1-4fbd-9b4c-ab87a4056fe4n%40googlegroups.com.

No comments:

Post a Comment