Ticket #13584: allow_empty_file_formfield.diff

File allow_empty_file_formfield.diff, 2.4 KB (added by Sasha Romijn, 14 years ago)
  • django/forms/fields.py

     
    439439
    440440    def __init__(self, *args, **kwargs):
    441441        self.max_length = kwargs.pop('max_length', None)
     442        self.allow_empty_file = kwargs.pop('allow_empty_file', False)
    442443        super(FileField, self).__init__(*args, **kwargs)
    443444
    444445    def to_python(self, data):
     
    457458            raise ValidationError(self.error_messages['max_length'] % error_values)
    458459        if not file_name:
    459460            raise ValidationError(self.error_messages['invalid'])
    460         if not file_size:
     461        if not self.allow_empty_file and not file_size:
    461462            raise ValidationError(self.error_messages['empty'])
    462463
    463464        return data
  • tests/regressiontests/forms/fields.py

     
    463463        self.assertEqual('files/test2.pdf', f.clean(None, 'files/test2.pdf'))
    464464        self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content'))))
    465465
     466    def test_filefield_ticket13584(self):
     467        f = FileField(allow_empty_file = True)
     468        self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', ''))))
     469
    466470    # URLField ##################################################################
    467471
    468472    def test_urlfield_37(self):
  • docs/ref/forms/fields.txt

     
    513513    * Empty value: ``None``
    514514    * Normalizes to: An ``UploadedFile`` object that wraps the file content
    515515      and file name into a single object.
    516     * Validates that non-empty file data has been bound to the form.
     516    * Can validate that non-empty file data has been bound to the form.
    517517    * Error message keys: ``required``, ``invalid``, ``missing``, ``empty``
    518518
     519Has one optional argument for validation, ``allow_empty_file``. If provided,
     520validation will succeed even if the file content is empty.
     521
    519522To learn more about the ``UploadedFile`` object, see the :ref:`file uploads
    520523documentation <topics-http-file-uploads>`.
    521524
Back to Top