Opened 16 years ago
Closed 12 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)
Change History (20)
comment:1 by , 16 years ago
Summary: | "unique" in error_messages of ModelForm → Make the "must be unique" error messages in ModelForms customizable |
---|
comment:2 by , 16 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 by , 15 years ago
Cc: | added |
---|
comment:4 by , 15 years ago
Cc: | added |
---|
comment:5 by , 14 years ago
Cc: | added |
---|
follow-up: 7 comment:6 by , 14 years ago
Easy pickings: | unset |
---|---|
Severity: | → Normal |
Type: | → Uncategorized |
Is this something appropriate for Django 1.4 now?
comment:7 by , 14 years ago
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 by , 14 years ago
Cc: | added |
---|
comment:9 by , 13 years ago
Owner: | changed from | to
---|---|
UI/UX: | unset |
by , 13 years ago
Attachment: | unique-error-message.diff added |
---|
comment:10 by , 13 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 , 13 years ago
You've got a bare except on #168, with a ambiguous failure message. I'd just remove that altogether.
comment:12 by , 13 years ago
Needs documentation: | unset |
---|
comment:13 by , 13 years ago
Patch needs improvement: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
comment:14 by , 13 years ago
I've created a new ticket (#16192) for unique_together custom error messages since this is more difficult.
by , 13 years ago
Attachment: | unique-error-message-with-docs-author-update.diff added |
---|
Updated my email address in AUTHORS
comment:16 by , 13 years ago
Cc: | 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 by , 12 years ago
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