From 71093d22b62548fc2667468f1ae9e28f4fef30db Mon Sep 17 00:00:00 2001 From: Loic Bistuer Date: Tue, 6 Aug 2013 02:26:51 +0700 Subject: [PATCH] Fixed #16986 -- Model.clean() can report errors on individual fields. This commit provides the tests for this issue but the actual problem was solved by the ValidationError refactor in f34cfec and ee77d4b. Refs #20199. --- tests/model_forms/models.py | 6 +++++- tests/model_forms/tests.py | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/model_forms/models.py b/tests/model_forms/models.py index a4cf9471de1..17aec290558 100644 --- a/tests/model_forms/models.py +++ b/tests/model_forms/models.py @@ -12,7 +12,7 @@ import os import tempfile from django.core import validators -from django.core.exceptions import ImproperlyConfigured +from django.core.exceptions import ImproperlyConfigured, ValidationError from django.core.files.storage import FileSystemStorage from django.db import models from django.utils import six @@ -296,3 +296,7 @@ class CustomErrorMessage(models.Model): name2 = models.CharField(max_length=50, validators=[validators.validate_slug], error_messages={'invalid': 'Model custom error message.'}) + + def clean(self): + if self.name1 == 'FORBIDDEN_VALUE': + raise ValidationError({'name1': [ValidationError('Model.clean() error messages.')]}) diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index 21e3143be64..14047254844 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -1782,6 +1782,14 @@ class OldFormForXTests(TestCase): '' ) + def test_model_clean_error_messages(self) : + data = {'name1': 'FORBIDDEN_VALUE', 'name2': 'ABC'} + errors = CustomErrorMessageForm(data).errors + self.assertHTMLEqual( + str(errors['name1']), + '' + ) + class M2mHelpTextTest(TestCase): """Tests for ticket #9321."""