Friday, December 11, 2020

Problem re-rendering ClearableFileInput on validation error

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+unsubscribe@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.

No comments:

Post a Comment