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

Remember FlexibleImageWidget choice on form redisplay #3681

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from

Conversation

ammar257ammar
Copy link
Contributor

closes #3558

@ammar257ammar ammar257ammar changed the title Remember selected image in FlexibleImageWidget on form redisplay upon validation error Remember FlexibleImageWidget choice on form redisplay Nov 2, 2024
@ammar257ammar ammar257ammar self-assigned this Nov 4, 2024
hx-include="[id='values-{{ widget.name }}']"
>
{% if widget.attrs.current_value %}
<option value="{{ widget.attrs.current_value.image.pk }}">{{ widget.attrs.current_value }}</option>
<option value="{{ widget.attrs.current_value.pk }}">{{ widget.attrs.current_value.title }}</option>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my understanding: we don't actually use the value of this anywhere, do we? Could it then just be left empty or set to one of the two widget choice types below (depending on the type)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This value is sent on submission in edit views (where the image is already saved in database) and in form redisplay upon validation error.

Copy link
Contributor Author

@ammar257ammar ammar257ammar Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the change in Add WidgetChoice for selected image for semantic consistency
It is not much change but semantically makes more sense instead of printing the pk value without using it anywhere.

Image.objects.filter(pk=current_value).exists()
or UserUpload.objects.filter(pk=current_value).exists()
elif (
widget_name == WidgetChoices.IMAGE_SELECTED.name
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part of the condition will work anyway without checking for widget_name value, but it is more robust to check for it.

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

Successfully merging this pull request may close these issues.

FlexibleImageWidget resets when form is re-rendered due to validation error on another form field
2 participants