Opened 15 years ago
Closed 11 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, mwarkentin, 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)
Change History (20)
comment:1 Changed 15 years ago by
Summary: | "unique" in error_messages of ModelForm → Make the "must be unique" error messages in ModelForms customizable |
---|
comment:2 Changed 14 years ago by
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 Changed 13 years ago by
Cc: | Oroku Saki added |
---|
comment:4 Changed 13 years ago by
Cc: | tinodb added |
---|
comment:5 Changed 12 years ago by
Cc: | mwarkentin added |
---|
comment:6 follow-up: 7 Changed 12 years ago by
Easy pickings: | unset |
---|---|
Severity: | → Normal |
Type: | → Uncategorized |
Is this something appropriate for Django 1.4 now?
comment:7 Changed 12 years ago by
Type: | Uncategorized → New 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 Changed 12 years ago by
Cc: | Matthew Schick added |
---|
comment:9 Changed 12 years ago by
Owner: | changed from nobody to Leah Culver |
---|---|
UI/UX: | unset |
Changed 12 years ago by
Attachment: | unique-error-message.diff added |
---|
comment:10 Changed 12 years ago by
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 Changed 12 years ago by
You've got a bare except on #168, with a ambiguous failure message. I'd just remove that altogether.
comment:12 Changed 12 years ago by
Needs documentation: | unset |
---|
comment:13 Changed 12 years ago by
Patch needs improvement: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
comment:14 Changed 12 years ago by
I've created a new ticket (#16192) for unique_together custom error messages since this is more difficult.
Changed 12 years ago by
Attachment: | unique-error-message-with-docs-author-update.diff added |
---|
Updated my email address in AUTHORS
comment:16 Changed 11 years ago by
Cc: | uznick@… added |
---|---|
Has patch: | unset |
Patch needs improvement: | set |
Resolution: | fixed |
Status: | closed → reopened |
Type: | New feature → Bug |
Version: | 1.0 → SVN |
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 Changed 11 years ago by
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
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.
See this thread before applying a patch. I'll work on a patch after 1.2 release is complete.
orokusaki