mirror of
https://github.com/django/django.git
synced 2025-10-27 23:56:08 +00:00
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
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user