Fixed #19997 -- Added custom EMPTY_VALUES to form fields

Thanks Loic Bistuer for the report and the patch.
This commit is contained in:
Claude Paroz 2013-03-07 09:21:59 +01:00
parent 25ce177e66
commit 4cccb85e29
6 changed files with 50 additions and 30 deletions

View file

@ -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' : {}})