mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Fixed #20199 -- Allow ModelForm fields to override error_messages from model fields
This commit is contained in:
@@ -11,6 +11,7 @@ from __future__ import unicode_literals
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
from django.core import validators
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.core.files.storage import FileSystemStorage
|
||||
from django.db import models
|
||||
@@ -286,3 +287,12 @@ class ColourfulItem(models.Model):
|
||||
class ArticleStatusNote(models.Model):
|
||||
name = models.CharField(max_length=20)
|
||||
status = models.ManyToManyField(ArticleStatus)
|
||||
|
||||
class CustomErrorMessage(models.Model):
|
||||
name1 = models.CharField(max_length=50,
|
||||
validators=[validators.validate_slug],
|
||||
error_messages={'invalid': 'Model custom error message.'})
|
||||
|
||||
name2 = models.CharField(max_length=50,
|
||||
validators=[validators.validate_slug],
|
||||
error_messages={'invalid': 'Model custom error message.'})
|
||||
|
||||
@@ -22,7 +22,7 @@ from .models import (Article, ArticleStatus, BetterWriter, BigInt, Book,
|
||||
DerivedPost, ExplicitPK, FlexibleDatePost, ImprovedArticle,
|
||||
ImprovedArticleWithParentLink, Inventory, Post, Price,
|
||||
Product, TextFile, Writer, WriterProfile, Colour, ColourfulItem,
|
||||
ArticleStatusNote, DateTimePost, test_images)
|
||||
ArticleStatusNote, DateTimePost, CustomErrorMessage, test_images)
|
||||
|
||||
if test_images:
|
||||
from .models import ImageFile, OptionalImageFile
|
||||
@@ -252,6 +252,12 @@ class StatusNoteCBM2mForm(forms.ModelForm):
|
||||
fields = '__all__'
|
||||
widgets = {'status': forms.CheckboxSelectMultiple}
|
||||
|
||||
class CustomErrorMessageForm(forms.ModelForm):
|
||||
name1 = forms.CharField(error_messages={'invalid': 'Form custom error message.'})
|
||||
|
||||
class Meta:
|
||||
model = CustomErrorMessage
|
||||
|
||||
|
||||
class ModelFormBaseTest(TestCase):
|
||||
def test_base_form(self):
|
||||
@@ -1762,6 +1768,18 @@ class OldFormForXTests(TestCase):
|
||||
</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>"""
|
||||
% {'blue_pk': colour.pk})
|
||||
|
||||
def test_custom_error_messages(self) :
|
||||
data = {'name1': '@#$!!**@#$', 'name2': '@#$!!**@#$'}
|
||||
errors = CustomErrorMessageForm(data).errors
|
||||
self.assertHTMLEqual(
|
||||
str(errors['name1']),
|
||||
'<ul class="errorlist"><li>Form custom error message.</li></ul>'
|
||||
)
|
||||
self.assertHTMLEqual(
|
||||
str(errors['name2']),
|
||||
'<ul class="errorlist"><li>Model custom error message.</li></ul>'
|
||||
)
|
||||
|
||||
|
||||
class M2mHelpTextTest(TestCase):
|
||||
"""Tests for ticket #9321."""
|
||||
|
||||
@@ -214,8 +214,8 @@ class TestSimpleValidators(TestCase):
|
||||
|
||||
def test_message_dict(self):
|
||||
v = ValidationError({'first': ['First Problem']})
|
||||
self.assertEqual(str(v), str_prefix("{%(_)s'first': %(_)s'First Problem'}"))
|
||||
self.assertEqual(repr(v), str_prefix("ValidationError({%(_)s'first': %(_)s'First Problem'})"))
|
||||
self.assertEqual(str(v), str_prefix("{%(_)s'first': [%(_)s'First Problem']}"))
|
||||
self.assertEqual(repr(v), str_prefix("ValidationError({%(_)s'first': [%(_)s'First Problem']})"))
|
||||
|
||||
test_counter = 0
|
||||
for validator, value, expected in TEST_DATA:
|
||||
|
||||
Reference in New Issue
Block a user