Skip to content

Commit 759e251

Browse files
committed
Add ImproperlyConfigured exception
1 parent 5442d98 commit 759e251

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

django_mongodb_backend/forms/fields/array.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from itertools import chain
33

44
from django import forms
5-
from django.core.exceptions import ValidationError
5+
from django.core.exceptions import ImproperlyConfigured, ValidationError
66
from django.utils.translation import gettext_lazy as _
77

88
from ...utils import prefix_validation_error
@@ -20,6 +20,11 @@ def __init__(
2020
self.base_field = base_field
2121
self.delimiter = delimiter
2222
super().__init__(**kwargs)
23+
if (min_length is not None or max_length is not None) and size is not None:
24+
raise ImproperlyConfigured(
25+
"SimpleArrayField param 'size' cannot be "
26+
"specified with 'max_length' or 'min_length'."
27+
)
2328
if min_length is not None:
2429
self.min_length = min_length
2530
self.validators.append(ArrayMinLengthValidator(int(min_length)))

tests/forms_tests_/test_array.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ def test_required(self):
145145
field.clean("")
146146
self.assertEqual(cm.exception.messages[0], "This field is required.")
147147

148+
def test_misconfigured(self):
149+
msg = "SimpleArrayField param 'size' cannot be specified with 'max_length' or 'min_length'."
150+
with self.assertRaises(exceptions.ImproperlyConfigured) as cm:
151+
SimpleArrayField(forms.CharField(), max_length=3, size=2)
152+
self.assertEqual(cm.exception.args[0], msg)
153+
with self.assertRaises(exceptions.ImproperlyConfigured) as cm:
154+
SimpleArrayField(forms.CharField(), min_length=3, size=2)
155+
self.assertEqual(cm.exception.args[0], msg)
156+
148157
def test_model_field_formfield(self):
149158
model_field = ArrayField(models.CharField(max_length=27))
150159
form_field = model_field.formfield()

0 commit comments

Comments
 (0)