From 5b26a014a81ba0d404d46e11d2b45c01d92b97e5 Mon Sep 17 00:00:00 2001 From: Alasdair Nicol Date: Tue, 18 Nov 2014 17:58:43 +0000 Subject: Fixed #23865 -- documented how to assign errors to a field in Model.clean() Also added a unit test wit the simpler syntax which we have documented, where the dictionary values are strings. --- tests/model_forms/models.py | 2 ++ tests/model_forms/tests.py | 7 +++++++ 2 files changed, 9 insertions(+) (limited to 'tests/model_forms') diff --git a/tests/model_forms/models.py b/tests/model_forms/models.py index 1b5ae90b07..4ab681d1ae 100644 --- a/tests/model_forms/models.py +++ b/tests/model_forms/models.py @@ -381,6 +381,8 @@ class CustomErrorMessage(models.Model): def clean(self): if self.name1 == 'FORBIDDEN_VALUE': raise ValidationError({'name1': [ValidationError('Model.clean() error messages.')]}) + elif self.name1 == 'FORBIDDEN_VALUE2': + raise ValidationError({'name1': 'Model.clean() error messages (simpler syntax).'}) elif self.name1 == 'GLOBAL_ERROR': raise ValidationError("Global error message.") diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index 962e5b969f..4ff900e1b2 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -2269,6 +2269,13 @@ class ModelFormCustomErrorTests(TestCase): str(form.errors['name1']), '' ) + data = {'name1': 'FORBIDDEN_VALUE2', 'name2': 'ABC'} + form = CustomErrorMessageForm(data) + self.assertFalse(form.is_valid()) + self.assertHTMLEqual( + str(form.errors['name1']), + '' + ) data = {'name1': 'GLOBAL_ERROR', 'name2': 'ABC'} form = CustomErrorMessageForm(data) self.assertFalse(form.is_valid()) -- cgit v1.3