mirror of
https://github.com/django/django.git
synced 2025-08-04 10:59:45 +00:00
Fixed #26905 -- Allowed using MultiValueDict-like objects as form data.
This commit is contained in:
parent
2b759c94c5
commit
74bb013cc1
2 changed files with 27 additions and 7 deletions
|
@ -41,6 +41,11 @@ class PersonNew(Form):
|
|||
birthday = DateField()
|
||||
|
||||
|
||||
class MultiValueDictLike(dict):
|
||||
def getlist(self, key):
|
||||
return [self[key]]
|
||||
|
||||
|
||||
class FormsTestCase(SimpleTestCase):
|
||||
# A Form is a collection of Fields. It knows how to validate a set of data and it
|
||||
# knows how to render itself in a couple of default ways (e.g., an HTML table).
|
||||
|
@ -867,6 +872,12 @@ Java</label></li>
|
|||
f = SongForm(data)
|
||||
self.assertEqual(f.errors, {})
|
||||
|
||||
# SelectMultiple uses ducktyping so that MultiValueDictLike.getlist()
|
||||
# is called.
|
||||
f = SongForm(MultiValueDictLike({'name': 'Yesterday', 'composers': 'J'}))
|
||||
self.assertEqual(f.errors, {})
|
||||
self.assertEqual(f.cleaned_data['composers'], ['J'])
|
||||
|
||||
def test_multiple_hidden(self):
|
||||
class SongForm(Form):
|
||||
name = CharField()
|
||||
|
@ -904,6 +915,12 @@ Java</label></li>
|
|||
self.assertEqual(f.cleaned_data['composers'], ['J', 'P'])
|
||||
self.assertEqual(f.cleaned_data['name'], 'Yesterday')
|
||||
|
||||
# MultipleHiddenInput uses ducktyping so that
|
||||
# MultiValueDictLike.getlist() is called.
|
||||
f = SongForm(MultiValueDictLike({'name': 'Yesterday', 'composers': 'J'}))
|
||||
self.assertEqual(f.errors, {})
|
||||
self.assertEqual(f.cleaned_data['composers'], ['J'])
|
||||
|
||||
def test_escaping(self):
|
||||
# Validation errors are HTML-escaped when output as HTML.
|
||||
class EscapingForm(Form):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue