mirror of
https://github.com/django/django.git
synced 2025-07-07 11:19:12 +00:00
[soc2009/model-validation] Make sure that all validators in the same group (simple/complex) get run even if they all fail and we get all their messages.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@11185 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
8424c8ae6c
commit
63f244f144
@ -254,6 +254,7 @@ class BaseForm(StrAndUnicode):
|
||||
for name, field in self.fields.items():
|
||||
if not name in self.cleaned_data:
|
||||
continue
|
||||
failed = False
|
||||
for v in field.validators:
|
||||
# skip noncomplex validators, they have already been run on the Field
|
||||
if not isinstance(v, ComplexValidator):
|
||||
@ -261,14 +262,14 @@ class BaseForm(StrAndUnicode):
|
||||
try:
|
||||
v(self.cleaned_data[name], all_values=self.cleaned_data)
|
||||
except ValidationError, e:
|
||||
failed = True
|
||||
error_list = self._errors.setdefault(name, self.error_class())
|
||||
if hasattr(e, 'code'):
|
||||
error_list.append(field.error_messages.get(e.code, e.messages[0]))
|
||||
else:
|
||||
error_list.extend(e.messages)
|
||||
if name in self.cleaned_data:
|
||||
del self.cleaned_data[name]
|
||||
|
||||
if failed:
|
||||
del self.cleaned_data[name]
|
||||
try:
|
||||
self.cleaned_data = self.clean()
|
||||
except ValidationError, e:
|
||||
|
@ -32,6 +32,7 @@ from formsets import tests as formset_tests
|
||||
from media import media_tests
|
||||
|
||||
from fields import TestFields
|
||||
from validators import TestFormWithValidators, TestFieldWithValidators
|
||||
|
||||
__test__ = {
|
||||
'extra_tests': extra_tests,
|
||||
|
34
tests/regressiontests/forms/validators.py
Normal file
34
tests/regressiontests/forms/validators.py
Normal file
@ -0,0 +1,34 @@
|
||||
from unittest import TestCase
|
||||
|
||||
from django import forms
|
||||
from django.core import validators
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
class AlwaysFailingValidator(validators.ComplexValidator):
|
||||
def __call__(self, value, all_values={}, obj=None):
|
||||
raise ValidationError('AlwaysFailingValidator')
|
||||
|
||||
class TestFieldWithValidators(TestCase):
|
||||
def test_all_errors_get_reported(self):
|
||||
field = forms.CharField(
|
||||
validators=[validators.validate_integer, validators.validate_email,]
|
||||
)
|
||||
self.assertRaises(ValidationError, field.clean, 'not int nor mail')
|
||||
try:
|
||||
field.clean('not int nor mail')
|
||||
except ValidationError, e:
|
||||
self.assertEqual(2, len(e.messages))
|
||||
|
||||
class TestFormWithValidators(TestCase):
|
||||
def test_all_complex_validators_get_run_even_if_they_fail(self):
|
||||
class MyForm(forms.Form):
|
||||
validator_field = forms.CharField(
|
||||
validators=[
|
||||
AlwaysFailingValidator(),
|
||||
AlwaysFailingValidator(),
|
||||
]
|
||||
)
|
||||
form = MyForm({'validator_field': 'some value'})
|
||||
self.assertFalse(form.is_valid())
|
||||
self.assertEqual(['validator_field'], form.errors.keys())
|
||||
self.assertEqual(['AlwaysFailingValidator', 'AlwaysFailingValidator'], form.errors['validator_field'])
|
Loading…
x
Reference in New Issue
Block a user