1
0
mirror of https://github.com/django/django.git synced 2024-12-24 01:55:49 +00:00
django/tests/forms_tests/tests/test_validators.py
2013-07-01 14:29:33 +02:00

41 lines
1.1 KiB
Python

from __future__ import unicode_literals
from unittest import TestCase
from django import forms
from django.core import validators
from django.core.exceptions import ValidationError
class UserForm(forms.Form):
full_name = forms.CharField(
max_length = 50,
validators = [
validators.validate_integer,
validators.validate_email,
]
)
string = forms.CharField(
max_length = 50,
validators = [
validators.RegexValidator(
regex='^[a-zA-Z]*$',
message="Letters only.",
)
]
)
class TestFieldWithValidators(TestCase):
def test_all_errors_get_reported(self):
form = UserForm({'full_name': 'not int nor mail', 'string': '2 is not correct'})
self.assertRaises(ValidationError, form.fields['full_name'].clean, 'not int nor mail')
try:
form.fields['full_name'].clean('not int nor mail')
except ValidationError as e:
self.assertEqual(2, len(e.messages))
self.assertFalse(form.is_valid())
self.assertEqual(form.errors['string'], ["Letters only."])