From 4cccb85e292fea01b3459cd97d751ed35179a7b7 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Thu, 7 Mar 2013 09:21:59 +0100 Subject: Fixed #19997 -- Added custom EMPTY_VALUES to form fields Thanks Loic Bistuer for the report and the patch. --- tests/forms_tests/tests/forms.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'tests/forms_tests') diff --git a/tests/forms_tests/tests/forms.py b/tests/forms_tests/tests/forms.py index f856e30d33..9cf217b86b 100644 --- a/tests/forms_tests/tests/forms.py +++ b/tests/forms_tests/tests/forms.py @@ -1797,3 +1797,23 @@ class FormsTestCase(TestCase): form = NameForm(data={'name' : ['fname', 'lname']}) self.assertTrue(form.is_valid()) self.assertEqual(form.cleaned_data, {'name' : 'fname lname'}) + + def test_custom_empty_values(self): + """ + Test that form fields can customize what is considered as an empty value + for themselves (#19997). + """ + class CustomJSONField(CharField): + empty_values = [None, ''] + def to_python(self, value): + # Fake json.loads + if value == '{}': + return {} + return super(CustomJSONField, self).to_python(value) + + class JSONForm(forms.Form): + json = CustomJSONField() + + form = JSONForm(data={'json': '{}'}); + form.full_clean() + self.assertEqual(form.cleaned_data, {'json' : {}}) -- cgit v1.3