mirror of
https://github.com/django/django.git
synced 2025-11-03 21:25:09 +00:00
Fixed #19997 -- Added custom EMPTY_VALUES to form fields
Thanks Loic Bistuer for the report and the patch.
This commit is contained in:
parent
25ce177e66
commit
4cccb85e29
6 changed files with 50 additions and 30 deletions
|
|
@ -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' : {}})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue