Opened 5 years ago

Last modified 5 years ago

#30014 closed Bug

Initialising disabled ModelChoiceField yields 'Select a valid choice'-error despite initialised option being valid — at Version 1

Reported by: Thomas Hamann Owned by: nobody
Component: Forms Version: dev
Severity: Normal Keywords: forms, disabled field, error, to_field_name
Cc: Etienne Chove Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Thomas Hamann)

I have a form with a ModelChoiceField that gets initialised to a specific value using get_initial in that form's View. This value is a valid choice for that Model. I don't want the user to be able to change the option on the form, but it needs to be displayed nonetheless.

When I set disabled=True on that field in forms.py, submitting the form yields the following error:

<ul class="errorlist"><li>fieldname<ul class="errorlist"><li>Select a valid choice. That choice is not one of the available choices.</li></ul></li></ul>.

Firstly, I would like to comment on the general quality of the error message, as it is not very useful: It does not return which choice it considers invalid. Including this information would make the message much more informative, and would avoid sending people on a wild goose chase to discover what the message could possibly mean.

Secondly, if a field is disabled but does contain a valid choice, validating the form should work and not trigger an error.

Edit: Adding the to_field_name option to the form field fixes the problem. However, when disabled=True is not present, this is not required.

This is probably related to the bugfix for this bug: https://code.djangoproject.com/ticket/28387

Change History (1)

comment:1 by Thomas Hamann, 5 years ago

Description: modified (diff)
Keywords: to_field_name added
Note: See TracTickets for help on using tickets.
Back to Top