Opened 10 years ago
Closed 10 years ago
#24010 closed New feature (needsinfo)
Unable to use choice values instead of choice keys in form/field validation
Reported by: | László Károlyi | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 1.7 |
Severity: | Normal | Keywords: | field choices error display |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hey,
In django.forms.fields.TypedChoiceField
and in django.db.models.fields.Field
I'd like to have the ability of outputting the given choice value instead of the choice key in error messages (when error_messages['invalid_choice']
is used), if a choice value is available.
This way, the end users could get a more understandable output when filling out a form with errors.
Change History (11)
comment:1 by , 10 years ago
Type: | Uncategorized → New feature |
---|
follow-up: 4 comment:2 by , 10 years ago
comment:3 by , 10 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
comment:4 by , 10 years ago
Exactly. This way, the choice names would not have been exposed for the user, because they might not make sense for him. However, exposing the choice value, which is often used as a label, would be valuable.
Replying to timgraham:
I'm not sure I understand -- could you give an example?
If you have a field like:
models.CharField(max_length=1, choices=(('A', 'ABC'), ('X', 'XYZ')))
but the POST data contains 'B', you want a different error message from "Select a valid choice. B is not one of the available choices."?
follow-up: 6 comment:5 by , 10 years ago
But there is no way to translate 'B' because it's not a valid choice?
comment:6 by , 10 years ago
Resolution: | needsinfo |
---|---|
Status: | closed → new |
Like I phrased, then output 'B'. If there's a vlue to the choice, output that.
Can't this be solved?
Replying to timgraham:
But there is no way to translate 'B' because it's not a valid choice?
follow-up: 8 comment:7 by , 10 years ago
If the value appears in choices
, isn't it a valid choice? If you could submit a test or some code to better demonstrate the problem that would be helpful.
comment:8 by , 10 years ago
Oh yes, I forgot that it's a special case, so let me elaborate. I created a form, where not all of the original choices are available for a field:
class MyForm(model.ModelForm): def __init__(self, choices, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) self.fields['option'].choices = [ x for x in choices.ALL_CHOICES if x[0] in choices]
Now, when cleaning the form, the option field can get an option which has a choice, but is not in the 'enabled' choice list. In that case, it'd be nice to see the corresponding value of choices.ALL_CHOICES.
Does that make sense?
Replying to timgraham:
If the value appears in
choices
, isn't it a valid choice? If you could submit a test or some code to better demonstrate the problem that would be helpful.
follow-up: 10 comment:9 by , 10 years ago
- Can you propose a patch that implements this? I'm not sure it's possible.
- If this choice isn't enabled on the form, how could a user submit it? Doesn't seem like it would be a very common problem.
comment:10 by , 10 years ago
The form generates all the choices, but hides the disabled ones, as it's a single-page ui, that according to what data it gets, enables or disables certain options.
I am quite new to this community (however not to python), and currently I'm in a huge project. But whenever I'll have time, I can submit something.
Until then, feel free to go ahead :)
comment:11 by , 10 years ago
Resolution: | → needsinfo |
---|---|
Status: | new → closed |
All right, closing for now as it's not clear to me how this can be implemented. Please reopen when you have some code to share.
I'm not sure I understand -- could you give an example?
If you have a field like:
models.CharField(max_length=1, choices=(('A', 'ABC'), ('X', 'XYZ')))
but the POST data contains 'B', you want a different error message from "Select a valid choice. B is not one of the available choices."?