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 László Károlyi, 10 years ago

Type: UncategorizedNew feature

comment:2 by Tim Graham, 10 years ago

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."?

comment:3 by Tim Graham, 10 years ago

Resolution: needsinfo
Status: newclosed

in reply to:  2 comment:4 by László Károlyi, 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."?

comment:5 by Tim Graham, 10 years ago

But there is no way to translate 'B' because it's not a valid choice?

in reply to:  5 comment:6 by László Károlyi, 10 years ago

Resolution: needsinfo
Status: closednew

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?

comment:7 by Tim Graham, 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.

in reply to:  7 comment:8 by László Károlyi, 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.

comment:9 by Tim Graham, 10 years ago

  1. Can you propose a patch that implements this? I'm not sure it's possible.
  2. 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.

in reply to:  9 comment:10 by László Károlyi, 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 Tim Graham, 10 years ago

Resolution: needsinfo
Status: newclosed

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.

Note: See TracTickets for help on using tickets.
Back to Top