Opened 2 years ago

Closed 15 months ago

Last modified 15 months 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


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/ (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 claudep 18 months ago.
17840-2.diff (5.7 KB) - added by claudep 17 months ago.
Added ungettext_lazy for some messages

Download all attachments as: .zip

Change History (8)

comment:1 Changed 2 years ago by claudep

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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

Changed 18 months ago by claudep

comment:2 Changed 18 months ago by claudep

  • Has patch set

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

comment:3 Changed 17 months ago by claudep

See also #19832

Changed 17 months ago by claudep

Added ungettext_lazy for some messages

comment:4 Changed 17 months ago by claudep

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

comment:5 Changed 15 months ago by Claude Paroz <claude@…>

  • Resolution set to fixed
  • Status changed from new to closed

In be9ae693c46021fd3a70c0ec21dd566960b29ffb:

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

Also fixed plural messages for DecimalField.

comment:6 Changed 15 months ago by Claude Paroz <claude@…>

In 5306285ce21d59742d67a447c9c1a1e4be6d86a4:

Complemented documentation following commit be9ae693c

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

Add Comment

Modify Ticket

Change Properties
<Author field>
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.