Ticket #13584: 13584.diff

File 13584.diff, 2.5 KB (added by Flaviu Simihaian, 13 years ago)
  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index 4d7728f..f5dcbc4 100644
    a b class FileField(Field):  
    466466
    467467    def __init__(self, *args, **kwargs):
    468468        self.max_length = kwargs.pop('max_length', None)
     469        self.allow_empty_file = kwargs.pop('allow_empty_file', False)
    469470        super(FileField, self).__init__(*args, **kwargs)
    470471
    471472    def to_python(self, data):
    class FileField(Field):  
    484485            raise ValidationError(self.error_messages['max_length'] % error_values)
    485486        if not file_name:
    486487            raise ValidationError(self.error_messages['invalid'])
    487         if not file_size:
     488        if not self.allow_empty_file and not file_size:
    488489            raise ValidationError(self.error_messages['empty'])
    489490
    490491        return data
  • docs/ref/forms/fields.txt

    diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt
    index b49864f..e976b32 100644
    a b given length.  
    503503    * Empty value: ``None``
    504504    * Normalizes to: An ``UploadedFile`` object that wraps the file content
    505505      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.
    507507    * Error message keys: ``required``, ``invalid``, ``missing``, ``empty``,
    508508      ``max_length``
    509509
     510Has two optional arguments for validation, ''max_length'' and
     511''allow_empty_file''. If provided, these ensure that the file name is at
     512most the given length, and that validation will succeed even if the file
     513content is empty.
     514
    510515To learn more about the ``UploadedFile`` object, see the :doc:`file uploads
    511516documentation </topics/http/file-uploads>`.
    512517
  • tests/regressiontests/forms/tests/fields.py

    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):  
    506506        self.assertEqual('files/test2.pdf', f.clean(None, 'files/test2.pdf'))
    507507        self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', 'Some File Content'))))
    508508
     509    def test_filefield_3(self):
     510        f = FileField(allow_empty_file = True)
     511        self.assertEqual(SimpleUploadedFile, type(f.clean(SimpleUploadedFile('name', ''))))
     512       
    509513    # URLField ##################################################################
    510514
    511515    def test_urlfield_1(self):
Back to Top