diff options
| author | Joseph Kocherhans <joseph@jkocherhans.com> | 2009-03-30 22:56:05 +0000 |
|---|---|---|
| committer | Joseph Kocherhans <joseph@jkocherhans.com> | 2009-03-30 22:56:05 +0000 |
| commit | 22ac97b17cbc3491629f98b15cc9a699b14ebd73 (patch) | |
| tree | 1845663d43a471d3080068d034b1a78b52a1c5b0 /django/forms | |
| parent | 86842e21f41602f4f6fbf0e61bf25bc4bc8e9256 (diff) | |
[1.0.X] Fixed #10149. FileFields? in a form now validate max_length. Based on a patch by Massimo Scamarcia. Backport of r10227 from trunk.
git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10232 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 ccb54d8b32..d9f4890734 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -446,9 +446,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): @@ -465,6 +467,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: |
