mirror of
https://github.com/django/django.git
synced 2025-10-27 07:36:08 +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:
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'])
|
||||
Reference in New Issue
Block a user