summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHonza Král <honza.kral@gmail.com>2009-06-01 15:41:41 +0000
committerHonza Král <honza.kral@gmail.com>2009-06-01 15:41:41 +0000
commit32916adca861f8ed2a8498fa03b23cd050661a79 (patch)
tree204822f0960fc262ce771f13800c2deffdc50271
parente573fb41e95d7d3f825f0d58e21bb3803e5ac0cf (diff)
[soc2009/model-validation] Dont't validate already failed fields
This removes the duplicate messages we have been seeing git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@10876 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/contrib/auth/tests/forms.py4
-rw-r--r--django/db/models/base.py4
-rw-r--r--django/forms/models.py3
-rw-r--r--tests/modeltests/model_forms/models.py4
4 files changed, 8 insertions, 7 deletions
diff --git a/django/contrib/auth/tests/forms.py b/django/contrib/auth/tests/forms.py
index 38ee70b169..482979c59e 100644
--- a/django/contrib/auth/tests/forms.py
+++ b/django/contrib/auth/tests/forms.py
@@ -16,7 +16,7 @@ FORM_TESTS = """
>>> form.is_valid()
False
>>> form["username"].errors
-[u'A user with that username already exists.', u'This field cannot be blank.']
+[u'A user with that username already exists.']
# The username contains invalid data.
@@ -29,7 +29,7 @@ False
>>> form.is_valid()
False
>>> form["username"].errors
-[u'This value must contain only letters, numbers and underscores.', u'This field cannot be blank.']
+[u'This value must contain only letters, numbers and underscores.']
# The verification password is incorrect.
diff --git a/django/db/models/base.py b/django/db/models/base.py
index 32804c3622..8cccab340a 100644
--- a/django/db/models/base.py
+++ b/django/db/models/base.py
@@ -606,13 +606,15 @@ class Model(object):
"""
pass
- def clean(self):
+ def clean(self, exclude=[]):
"""
Cleans all fields and raises ValidationError containing message_dict of
all validation errors if any occur.
"""
errors = {}
for f in self._meta.fields:
+ if f.name in exclude:
+ continue
try:
# TODO: is the [sg]etattr correct?
setattr(self, f.attname, f.clean(getattr(self, f.attname), self))
diff --git a/django/forms/models.py b/django/forms/models.py
index 40cde06bc1..192ab8e8f4 100644
--- a/django/forms/models.py
+++ b/django/forms/models.py
@@ -243,8 +243,7 @@ class BaseModelForm(BaseForm):
self.instance = make_instance(self, self.instance, opts.fields, opts.exclude)
self.validate_unique()
try:
- # FIMXE: what to do about duplicate errors? (is required etc.)
- self.instance.clean()
+ self.instance.clean(exclude=self._errors.keys())
except ValidationError, e:
for k, v in e.message_dict.items():
if k != NON_FIELD_ERRORS:
diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
index a8a414143e..89fd56efe9 100644
--- a/tests/modeltests/model_forms/models.py
+++ b/tests/modeltests/model_forms/models.py
@@ -449,9 +449,9 @@ u'third-test'
If you call save() with invalid data, you'll get a ValueError.
>>> f = CategoryForm({'name': '', 'slug': 'not a slug!', 'url': 'foo'})
>>> f.errors['name']
-[u'This field is required.', u'This field cannot be blank.']
+[u'This field is required.']
>>> f.errors['slug']
-[u"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens.", u'This field cannot be blank.']
+[u"Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."]
>>> f.cleaned_data
Traceback (most recent call last):
...