Opened 16 years ago

Closed 13 years ago

#8913 closed Bug (fixed)

Make the "must be unique" error messages in ModelForms customizable

Reported by: Alexander_Nesterov Owned by: Leah Culver
Component: Forms Version: dev
Severity: Normal Keywords: forms, unique, validation
Cc: Oroku Saki, tinodb, Michael Warkentin, Matthew Schick, uznick@… Triage Stage: Ready for checkin
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

feature request (?)
model:

class Context(models.Model): 
    name        = models.CharField(unique=True)

form:

class ContextForm(forms.ModelForm): 
    name = forms.CharField() 
    class Meta: 
        model = Context

example:

form = ContextForm(request.POST)  
if form.is_valid(): 
    form.save()

Is there any way to override an error message of validate_unique: u"%(model_name)s with this %(field_label)s already exists."?

class ContextForm(forms.ModelForm): 
    name = forms.CharField(error_messages={'unique': u'My custom message'}) 

Attachments (3)

unique-error-message.diff (3.7 KB ) - added by Leah Culver 14 years ago.
unique-error-message-with-docs.diff (4.6 KB ) - added by Leah Culver 14 years ago.
Added documentation.
unique-error-message-with-docs-author-update.diff (5.0 KB ) - added by Leah Culver 14 years ago.
Updated my email address in AUTHORS

Download all attachments as: .zip

Change History (20)

comment:1 by anonymous, 16 years ago

Summary: "unique" in error_messages of ModelFormMake the "must be unique" error messages in ModelForms customizable

comment:2 by Jacob, 16 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Oroku Saki, 15 years ago

Cc: Oroku Saki added

See this thread before applying a patch. I'll work on a patch after 1.2 release is complete.

orokusaki

comment:4 by tinodb, 15 years ago

Cc: tinodb added

comment:5 by Michael Warkentin, 14 years ago

Cc: Michael Warkentin added

comment:6 by Anand Kumria, 14 years ago

Easy pickings: unset
Severity: Normal
Type: Uncategorized

Is this something appropriate for Django 1.4 now?

in reply to:  6 comment:7 by Julien Phalip, 14 years ago

Type: UncategorizedNew feature

Replying to wildfire:

Is this something appropriate for Django 1.4 now?

Not until there is a patch with tests and doc ;)

comment:8 by Matthew Schick, 14 years ago

Cc: Matthew Schick added

comment:9 by anonymous, 14 years ago

Owner: changed from nobody to Leah Culver
UI/UX: unset

by Leah Culver, 14 years ago

Attachment: unique-error-message.diff added

comment:10 by Leah Culver, 14 years ago

Has patch: set
Needs documentation: set
Patch needs improvement: set

I've added a rough patch for this. To specify a custom error message for the unique contraint, you'll need to do something like this:

class Context(models.Model): 
    name = models.CharField(error_messages={'unique': u'My custom message'}) 

I don't know a lot about ModelForms vs. Forms but I can't seem to get this to work for plain Forms. Should this also be a Form error_message key?

I'm also not sure where to put the docs since the Model error_messages parameter is documented but lacking specificity (https://docs.djangoproject.com/en/1.3/ref/models/fields/#error-messages). Should I make this more specific?

comment:11 by anonymous, 14 years ago

You've got a bare except on #168, with a ambiguous failure message. I'd just remove that altogether.

by Leah Culver, 14 years ago

Added documentation.

comment:12 by Leah Culver, 14 years ago

Needs documentation: unset

comment:13 by Andrew Godwin, 14 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:14 by Leah Culver, 14 years ago

I've created a new ticket (#16192) for unique_together custom error messages since this is more difficult.

by Leah Culver, 14 years ago

Updated my email address in AUTHORS

comment:15 by Andrew Godwin, 14 years ago

Resolution: fixed
Status: newclosed

In [16345]:

Fixed #8913 - Make "must be unique" error messages customisable. Thanks to Leah Culver.

comment:16 by Uznick <uznick@…>, 13 years ago

Cc: uznick@… added
Has patch: unset
Patch needs improvement: set
Resolution: fixed
Status: closedreopened
Type: New featureBug
Version: 1.0SVN

The error message is still not customizable when it's set in the ModelForm.

Example:

class InformationSubscriber(models.Model):
    email = models.EmailField(u"E-mail", unique=True)

class InformationSubscribeForm(forms.ModelForm):
    email = forms.EmailField(label=u"E-mail", error_messages={'unique': u"You are already subscribed to our news",})

When the form is submitted, the error is: Information Subscriber with this E-mail already exists.

And when I set the error_messages in the Model, like

class InformationSubscriber(models.Model):
    email = models.EmailField(u"E-mail", unique=True, error_messages={'unique': u"You are already subscribed to our news",})

class InformationSubscribeForm(forms.ModelForm):
    email = forms.EmailField(label=u"E-mail")

The error message is correct: You are already subscribed to our news.

But the problem is, that there should be an option to set a unique error message on the form-level, not on the level of the whole model itself.

comment:17 by Claude Paroz, 13 years ago

Resolution: fixed
Status: reopenedclosed

I would suggest to open a new ticket, as allowing the message to be set on the form level is a different story. Currently, validate_unique may produce several errors to pass to ValidationError, so we cannot simply set code and params to be able to catch it at the form level.

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