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

UneditableField renders all select options #817

Open
zulrang opened this issue May 30, 2018 · 5 comments · May be fixed by #934
Open

UneditableField renders all select options #817

zulrang opened this issue May 30, 2018 · 5 comments · May be fixed by #934

Comments

@zulrang
Copy link

zulrang commented May 30, 2018

  • Package version: 1.7.2
  • Django version: 1.11.13
  • Python version: 3.4.5
  • Template pack: bootstrap4

Description:

UneditableField still renders entire select dropdown with all options.

I have an UneditableField for an associated User to my model, and for both performance and security reasons, I don't want every user to be rendered to the page when all I want is the current value displayed.

@ociule
Copy link
Contributor

ociule commented Jun 6, 2018

This is interesting, but it's missing the code to reproduce.

Can you please post a form that repoduces the issue ? Or better yet, fork and add a branch reproducing the issue to https://github.com/django-crispy-forms/crispy-test-project

Thanks

@zulrang
Copy link
Author

zulrang commented Jun 7, 2018

https://github.com/zulrang/crispy-test-project/tree/test-uneditable-field

I added an UneditableField to the default form with two choices and an initial. The select is populated on the page with both options. This is a problem when there are many options or (as in my initial issue) it is a list of users.

@ociule
Copy link
Contributor

ociule commented Jun 8, 2018

I've looked at the templates (crispy_forms/templates/bootstrap{,3,4}/layout/uneditable_field.html) and this is a regression introduced in the bootstrap3 template. The "bootstrap" without version (I assume 2) template just dumps field.value:

<span {{ flat_attrs|safe }}>{% if field.value %}{{ field.value }}{% endif %}</span>

Note that this doesn't create a disabled select, it actually doesn't create an input at all. I agree with your reasoning about wanting to eliminate useless and invisible options from the uneditable select, but this bootstrap pre-3 approach is too backwards incompatible.

I think it would be best to have a new keyword argument "trim_invisible" for UneditableField that eliminates all but the seen options from an uneditable field. This would keep backwards compatibility and allow you to activate it to get what you want. This trim_invisible option could be implemented as above.

Activating trim_invisible by default is not a good idea, as it would break backwards compatibility. It seems to me it's a wide-spread enough usage to create a disabled select using UneditableField, then activate it in javascript as needed.

I hope to get feedback from core developers, as there are some design choices I've made that should be double checked.

@ociule
Copy link
Contributor

ociule commented Jun 8, 2018

I've implemented a trim_invisible_from_select option in this branch:

https://github.com/ociule/django-crispy-forms/tree/uneditable_trim_invisible_from_select

Can you test it out pls ?

To enable, you just set trim_invisible_from_select=True when creating an UneditableField.

@zoidyzoidzoid
Copy link
Contributor

Your implementation sounds sane to me, @ociule. Thanks for helping out.

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