diff --git a/django/forms/fields.py b/django/forms/fields.py
index 4d7728f..f5dcbc4 100644
a
|
b
|
class FileField(Field):
|
466 | 466 | |
467 | 467 | def __init__(self, *args, **kwargs): |
468 | 468 | self.max_length = kwargs.pop('max_length', None) |
| 469 | self.allow_empty_file = kwargs.pop('allow_empty_file', False) |
469 | 470 | super(FileField, self).__init__(*args, **kwargs) |
470 | 471 | |
471 | 472 | def to_python(self, data): |
… |
… |
class FileField(Field):
|
484 | 485 | raise ValidationError(self.error_messages['max_length'] % error_values) |
485 | 486 | if not file_name: |
486 | 487 | raise ValidationError(self.error_messages['invalid']) |
487 | | if not file_size: |
| 488 | if not self.allow_empty_file and not file_size: |
488 | 489 | raise ValidationError(self.error_messages['empty']) |
489 | 490 | |
490 | 491 | return data |
diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt
index b49864f..e976b32 100644
a
|
b
|
given length.
|
503 | 503 | * Empty value: ``None`` |
504 | 504 | * Normalizes to: An ``UploadedFile`` object that wraps the file content |
505 | 505 | and file name into a single object. |
506 | | * Validates that non-empty file data has been bound to the form. |
| 506 | * Can validate that non-empty file data has been bound to the form. |
507 | 507 | * Error message keys: ``required``, ``invalid``, ``missing``, ``empty``, |
508 | 508 | ``max_length`` |
509 | 509 | |
| 510 | Has two optional arguments for validation, ''max_length'' and |
| 511 | ''allow_empty_file''. If provided, these ensure that the file name is at |
| 512 | most the given length, and that validation will succeed even if the file |
| 513 | content is empty. |
| 514 | |
510 | 515 | To learn more about the ``UploadedFile`` object, see the :doc:`file uploads |
511 | 516 | documentation </topics/http/file-uploads>`. |
512 | 517 | |
diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py
index f76e732..c98d3b7 100644
a
|
b
|
class FieldsTests(TestCase):
|
506 | 506 | self.assertEqual('files/test2.pdf', f.clean(None, 'files/test2.pdf')) |
507 | 507 | self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content')))) |
508 | 508 | |
| 509 | def test_filefield_3(self): |
| 510 | f = FileField(allow_empty_file = True) |
| 511 | self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', '')))) |
| 512 | |
509 | 513 | # URLField ################################################################## |
510 | 514 | |
511 | 515 | def test_urlfield_1(self): |