diff options
| author | Joseph Kocherhans <joseph@jkocherhans.com> | 2009-03-30 22:52:16 +0000 |
|---|---|---|
| committer | Joseph Kocherhans <joseph@jkocherhans.com> | 2009-03-30 22:52:16 +0000 |
| commit | fea6c73538148a7362a9cdd85bbc4732ddb4204d (patch) | |
| tree | c1686c3f8f6960fb958992235a817ad39698aac5 /django/forms | |
| parent | 6ce25d3be9804ca199b65110d10240559293a917 (diff) | |
Fixed #10149. FileFields in a form now validate max_length. Based on a patch by Massimo Scamarcia.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10227 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/forms')
| -rw-r--r-- | django/forms/fields.py | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py index a414e76890..9c4084450a 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -447,9 +447,11 @@ class FileField(Field): 'invalid': _(u"No file was submitted. Check the encoding type on the form."), 'missing': _(u"No file was submitted."), 'empty': _(u"The submitted file is empty."), + 'max_length': _(u'Ensure this filename has at most %(max)d characters (it has %(length)d).'), } def __init__(self, *args, **kwargs): + self.max_length = kwargs.pop('max_length', None) super(FileField, self).__init__(*args, **kwargs) def clean(self, data, initial=None): @@ -466,6 +468,9 @@ class FileField(Field): except AttributeError: raise ValidationError(self.error_messages['invalid']) + if self.max_length is not None and len(file_name) > self.max_length: + error_values = {'max': self.max_length, 'length': len(file_name)} + raise ValidationError(self.error_messages['max_length'] % error_values) if not file_name: raise ValidationError(self.error_messages['invalid']) if not file_size: |
