Opened 4 years ago

Closed 4 years ago

#32347 closed New feature (fixed)

ModelChoiceField does not provide value of invalid choice when raising ValidationError

Reported by: Aaron Wiegel Owned by: Jerin Peter George
Component: Forms Version: 4.0
Severity: Normal Keywords:
Cc: 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 Aaron Wiegel)

Compared with ChoiceField and others, ModelChoiceField does not show the value of the invalid choice when raising a validation error. Passing in parameters with the invalid value and modifying the default error message for the code invalid_choice should fix this.

From source code:

class ModelMultipleChoiceField(ModelChoiceField):
    """A MultipleChoiceField whose choices are a model QuerySet."""
    widget = SelectMultiple
    hidden_widget = MultipleHiddenInput
    default_error_messages = {
        'invalid_list': _('Enter a list of values.'),
        'invalid_choice': _('Select a valid choice. %(value)s is not one of the'
                            ' available choices.'),
        'invalid_pk_value': _('“%(pk)s” is not a valid value.')
    }
    ...

class ModelChoiceField(ChoiceField):
    """A ChoiceField whose choices are a model QuerySet."""
    # This class is a subclass of ChoiceField for purity, but it doesn't
    # actually use any of ChoiceField's implementation.
    default_error_messages = {
        'invalid_choice': _('Select a valid choice. That choice is not one of'
                            ' the available choices.'),
    }
    ...

Change History (10)

comment:1 by Aaron Wiegel, 4 years ago

Description: modified (diff)

comment:2 by Aaron Wiegel, 4 years ago

Owner: changed from nobody to Aaron Wiegel
Status: newassigned

comment:3 by Carlton Gibson, 4 years ago

Resolution: wontfix
Status: assignedclosed

This message has been the same literally forever b2b6fc8e3c78671c8b6af2709358c3213c84d119.

Given that ChoiceField passes the value when raising the error, if you set error_messages you should be able to get the result you want.

in reply to:  3 comment:4 by Aaron Wiegel, 4 years ago

Replying to Carlton Gibson:

This message has been the same literally forever b2b6fc8e3c78671c8b6af2709358c3213c84d119.

Given that ChoiceField passes the value when raising the error, if you set error_messages you should be able to get the result you want.

That is ChoiceField. ModelChoiceField does not pass the value to the validation error. So, when the invalid value error is raised, you can't display the offending value even if you override the defaults.

Last edited 4 years ago by Aaron Wiegel (previous) (diff)

comment:5 by Aaron Wiegel, 4 years ago

Resolution: wontfix
Status: closednew

comment:6 by Carlton Gibson, 4 years ago

Easy pickings: unset
Triage Stage: UnreviewedAccepted
Type: BugNew feature

OK, if you want to look at submitting a PR we can see if any objections come up in review. Thanks.

comment:7 by Jerin Peter George, 4 years ago

Has patch: set
Owner: changed from Aaron Wiegel to Jerin Peter George
Status: newassigned

comment:8 by Mariusz Felisiak, 4 years ago

Needs documentation: set
Patch needs improvement: set

comment:9 by Mariusz Felisiak, 4 years ago

Needs documentation: unset
Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin
Version: 3.14.0

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 1adc0906:

Fixed #32347 -- Made ModelChoiceField include the value in ValidationError for invalid_choice.

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