Ticket #13584: allow_empty_file_formfield.diff
File allow_empty_file_formfield.diff, 2.4 KB (added by , 14 years ago) |
---|
-
django/forms/fields.py
439 439 440 440 def __init__(self, *args, **kwargs): 441 441 self.max_length = kwargs.pop('max_length', None) 442 self.allow_empty_file = kwargs.pop('allow_empty_file', False) 442 443 super(FileField, self).__init__(*args, **kwargs) 443 444 444 445 def to_python(self, data): … … 457 458 raise ValidationError(self.error_messages['max_length'] % error_values) 458 459 if not file_name: 459 460 raise ValidationError(self.error_messages['invalid']) 460 if not file_size:461 if not self.allow_empty_file and not file_size: 461 462 raise ValidationError(self.error_messages['empty']) 462 463 463 464 return data -
tests/regressiontests/forms/fields.py
463 463 self.assertEqual('files/test2.pdf', f.clean(None, 'files/test2.pdf')) 464 464 self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content')))) 465 465 466 def test_filefield_ticket13584(self): 467 f = FileField(allow_empty_file = True) 468 self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', '')))) 469 466 470 # URLField ################################################################## 467 471 468 472 def test_urlfield_37(self): -
docs/ref/forms/fields.txt
513 513 * Empty value: ``None`` 514 514 * Normalizes to: An ``UploadedFile`` object that wraps the file content 515 515 and file name into a single object. 516 * Validatesthat non-empty file data has been bound to the form.516 * Can validate that non-empty file data has been bound to the form. 517 517 * Error message keys: ``required``, ``invalid``, ``missing``, ``empty`` 518 518 519 Has one optional argument for validation, ``allow_empty_file``. If provided, 520 validation will succeed even if the file content is empty. 521 519 522 To learn more about the ``UploadedFile`` object, see the :ref:`file uploads 520 523 documentation <topics-http-file-uploads>`. 521 524