diff options
| author | Adrian Holovaty <adrian@holovaty.com> | 2006-12-15 23:07:41 +0000 |
|---|---|---|
| committer | Adrian Holovaty <adrian@holovaty.com> | 2006-12-15 23:07:41 +0000 |
| commit | 26489d4e2aa2a5d06b228643dfc232c05bb47872 (patch) | |
| tree | 44ffa04d0937bf581d85d6710dfd12cc0a2d4f4f | |
| parent | 4f5170bffe8ca6ce8da0f68c4834066b0c766b43 (diff) | |
newforms: Fixed unexpected behavior with CharField(required=False, min_length=X). Thanks for reporting, Benjamin Slavin
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4217 bcc190cf-cafb-0310-a4f2-bffc1f526a37
| -rw-r--r-- | django/newforms/fields.py | 5 | ||||
| -rw-r--r-- | tests/regressiontests/forms/tests.py | 12 |
2 files changed, 16 insertions, 1 deletions
diff --git a/django/newforms/fields.py b/django/newforms/fields.py index 24849bdbcf..af2533cec5 100644 --- a/django/newforms/fields.py +++ b/django/newforms/fields.py @@ -77,7 +77,10 @@ class CharField(Field): def clean(self, value): "Validates max_length and min_length. Returns a Unicode object." Field.clean(self, value) - if value in EMPTY_VALUES: value = u'' + if value in EMPTY_VALUES: + value = u'' + if not self.required: + return value value = smart_unicode(value) if self.max_length is not None and len(value) > self.max_length: raise ValidationError(gettext(u'Ensure this value has at most %d characters.') % self.max_length) diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index c75cedab14..e450561bd0 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -687,9 +687,21 @@ ValidationError: [u'Ensure this value has at most 10 characters.'] CharField accepts an optional min_length parameter: >>> f = CharField(min_length=10, required=False) >>> f.clean('') +u'' +>>> f.clean('12345') Traceback (most recent call last): ... ValidationError: [u'Ensure this value has at least 10 characters.'] +>>> f.clean('1234567890') +u'1234567890' +>>> f.clean('1234567890a') +u'1234567890a' + +>>> f = CharField(min_length=10, required=True) +>>> f.clean('') +Traceback (most recent call last): +... +ValidationError: [u'This field is required.'] >>> f.clean('12345') Traceback (most recent call last): ... |
