Opened 5 years ago

Closed 3 years ago

Last modified 3 years ago

#17840 closed Bug (fixed)

Overriding form field error messages without argument causes string formatting error.

Reported by: klein4 Owned by: nobody
Component: Forms Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Something like this in a form's init method

self.fields['my_choice_field'].error_messages['invalid_choice'] = u'Constant string'

will throw an error because of the following line in django/forms/models.py (line 1015):

raise ValidationError(self.error_messages['invalid_choice'] % val)

This method of string formatting forces the user's custom error message to contain a '%s' argument, like the default error message does. A dictionary-based string formatting operation like

self.error_messages['invalid_choice'] % {'foo': bar}

would fix this problem.

Attachments (2)

17840-1.diff (5.4 KB) - added by Claude Paroz 4 years ago.
17840-2.diff (5.7 KB) - added by Claude Paroz 4 years ago.
Added ungettext_lazy for some messages

Download all attachments as: .zip

Change History (8)

comment:1 Changed 5 years ago by Claude Paroz

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

Now that the 1.4 branch is string-frozen, this will have to wait for 1.5.

Changed 4 years ago by Claude Paroz

Attachment: 17840-1.diff added

comment:2 Changed 4 years ago by Claude Paroz

Has patch: set

I understand that this patch might be slightly backwards incompatible, but I think this needs to be fixed.

comment:3 Changed 4 years ago by Claude Paroz

See also #19832

Changed 4 years ago by Claude Paroz

Attachment: 17840-2.diff added

Added ungettext_lazy for some messages

comment:4 Changed 4 years ago by Claude Paroz

I updated the patch to include issues of ticket #19832.

comment:5 Changed 3 years ago by Claude Paroz <claude@…>

Resolution: fixed
Status: newclosed

In be9ae693c46021fd3a70c0ec21dd566960b29ffb:

Fixed #17840 -- Generalized named placeholders in form error messages

Also fixed plural messages for DecimalField.

comment:6 Changed 3 years ago by Claude Paroz <claude@…>

In 5306285ce21d59742d67a447c9c1a1e4be6d86a4:

Complemented documentation following commit be9ae693c

Refs #17840. Thanks Carl Meyer for noticing the omission.

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