Opened 13 years ago

Closed 12 years ago

Last modified 12 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 12 years ago.
17840-2.diff (5.7 KB ) - added by Claude Paroz 12 years ago.
Added ungettext_lazy for some messages

Download all attachments as: .zip

Change History (8)

comment:1 by Claude Paroz, 13 years ago

Triage Stage: UnreviewedAccepted

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

by Claude Paroz, 12 years ago

Attachment: 17840-1.diff added

comment:2 by Claude Paroz, 12 years ago

Has patch: set

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

comment:3 by Claude Paroz, 12 years ago

See also #19832

by Claude Paroz, 12 years ago

Attachment: 17840-2.diff added

Added ungettext_lazy for some messages

comment:4 by Claude Paroz, 12 years ago

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

comment:5 by Claude Paroz <claude@…>, 12 years ago

Resolution: fixed
Status: newclosed

In be9ae693c46021fd3a70c0ec21dd566960b29ffb:

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

Also fixed plural messages for DecimalField.

comment:6 by Claude Paroz <claude@…>, 12 years ago

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