Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #20440 -- Ensured CharField's max_length/min_length are integers

  • Loading branch information...
commit caf56ad1743778bad8af6b51b08f5baa342d4cd2 1 parent 0038296
@cvitan cvitan authored claudep committed
View
1  AUTHORS
@@ -164,6 +164,7 @@ answer newbie questions, and generally made Django that much better:
Leah Culver <leah.culver@gmail.com>
Raúl Cumplido <raulcumplido@gmail.com>
flavio.curella@gmail.com
+ Tome Cvitan <tome@cvitan.com>
John D'Agostino <john.dagostino@gmail.com>
dackze+django@gmail.com
Jim Dalton <jim.dalton@gmail.com>
View
14 django/forms/fields.py
@@ -198,14 +198,15 @@ def __deepcopy__(self, memo):
result.validators = self.validators[:]
return result
+
class CharField(Field):
def __init__(self, max_length=None, min_length=None, *args, **kwargs):
self.max_length, self.min_length = max_length, min_length
super(CharField, self).__init__(*args, **kwargs)
if min_length is not None:
- self.validators.append(validators.MinLengthValidator(min_length))
+ self.validators.append(validators.MinLengthValidator(int(min_length)))
if max_length is not None:
- self.validators.append(validators.MaxLengthValidator(max_length))
+ self.validators.append(validators.MaxLengthValidator(int(max_length)))
def to_python(self, value):
"Returns a Unicode object."
@@ -220,6 +221,7 @@ def widget_attrs(self, widget):
attrs.update({'maxlength': str(self.max_length)})
return attrs
+
class IntegerField(Field):
default_error_messages = {
'invalid': _('Enter a whole number.'),
@@ -444,6 +446,7 @@ def to_python(self, value):
def strptime(self, value, format):
return datetime.datetime.strptime(force_str(value), format).time()
+
class DateTimeField(BaseTemporalField):
widget = DateTimeInput
input_formats = formats.get_format_lazy('DATETIME_INPUT_FORMATS')
@@ -482,6 +485,7 @@ def to_python(self, value):
def strptime(self, value, format):
return datetime.datetime.strptime(force_str(value), format)
+
class RegexField(CharField):
def __init__(self, regex, max_length=None, min_length=None, error_message=None, *args, **kwargs):
"""
@@ -511,6 +515,7 @@ def _set_regex(self, regex):
regex = property(_get_regex, _set_regex)
+
class EmailField(CharField):
widget = EmailInput
default_validators = [validators.validate_email]
@@ -519,6 +524,7 @@ def clean(self, value):
value = self.to_python(value).strip()
return super(EmailField, self).clean(value)
+
class FileField(Field):
widget = ClearableFileInput
default_error_messages = {
@@ -626,6 +632,7 @@ def to_python(self, data):
f.seek(0)
return f
+
class URLField(CharField):
widget = URLInput
default_error_messages = {
@@ -792,6 +799,7 @@ def valid_value(self, value):
return True
return False
+
class TypedChoiceField(ChoiceField):
def __init__(self, *args, **kwargs):
self.coerce = kwargs.pop('coerce', lambda val: val)
@@ -903,6 +911,7 @@ def clean(self, value):
value = field.clean(value)
return value
+
class MultiValueField(Field):
"""
A Field that aggregates the logic of multiple Fields.
@@ -1047,6 +1056,7 @@ def __init__(self, path, match=None, recursive=False, allow_files=True,
self.widget.choices = self.choices
+
class SplitDateTimeField(MultiValueField):
widget = SplitDateTimeWidget
hidden_widget = SplitHiddenDateTimeWidget
View
9 tests/forms_tests/tests/test_fields.py
@@ -125,6 +125,15 @@ def test_charfield_5(self):
self.assertEqual(f.max_length, None)
self.assertEqual(f.min_length, 10)
+ def test_charfield_length_not_int(self):
+ """
+ Ensure that setting min_length or max_length to something that is not a
+ number returns an exception.
+ """
+ self.assertRaises(ValueError, CharField, min_length='a')
+ self.assertRaises(ValueError, CharField, max_length='a')
+ self.assertRaises(ValueError, CharField, 'a')
+
def test_charfield_widget_attrs(self):
"""
Ensure that CharField.widget_attrs() always returns a dictionary.
Please sign in to comment.
Something went wrong with that request. Please try again.