mirror of
https://github.com/django/django.git
synced 2025-09-01 00:08:13 +00:00
Fixed #25532 -- Properly redisplayed JSONField form input values
Thanks David Szotten for the report and Tommy Beadle for code inspiration. Thanks Tim Graham for the review.
This commit is contained in:
parent
64aba7a8ab
commit
db19619545
4 changed files with 52 additions and 4 deletions
|
@ -3,7 +3,9 @@ import unittest
|
|||
|
||||
from django.core import exceptions, serializers
|
||||
from django.db import connection
|
||||
from django.forms import CharField, Form
|
||||
from django.test import TestCase
|
||||
from django.utils.html import escape
|
||||
|
||||
from . import PostgreSQLTestCase
|
||||
from .models import JSONModel
|
||||
|
@ -258,7 +260,34 @@ class TestFormField(PostgreSQLTestCase):
|
|||
form_field = model_field.formfield()
|
||||
self.assertIsInstance(form_field, forms.JSONField)
|
||||
|
||||
def test_formfield_disabled(self):
|
||||
class JsonForm(Form):
|
||||
name = CharField()
|
||||
jfield = forms.JSONField(disabled=True)
|
||||
|
||||
form = JsonForm({'name': 'xyz', 'jfield': '["bar"]'}, initial={'jfield': ['foo']})
|
||||
self.assertIn('["foo"]</textarea>', form.as_p())
|
||||
|
||||
def test_prepare_value(self):
|
||||
field = forms.JSONField()
|
||||
self.assertEqual(field.prepare_value({'a': 'b'}), '{"a": "b"}')
|
||||
self.assertEqual(field.prepare_value(None), 'null')
|
||||
self.assertEqual(field.prepare_value('foo'), '"foo"')
|
||||
|
||||
def test_redisplay_wrong_input(self):
|
||||
"""
|
||||
When displaying a bound form (typically due to invalid input), the form
|
||||
should not overquote JSONField inputs.
|
||||
"""
|
||||
class JsonForm(Form):
|
||||
name = CharField(max_length=2)
|
||||
jfield = forms.JSONField()
|
||||
|
||||
# JSONField input is fine, name is too long
|
||||
form = JsonForm({'name': 'xyz', 'jfield': '["foo"]'})
|
||||
self.assertIn('["foo"]</textarea>', form.as_p())
|
||||
|
||||
# This time, the JSONField input is wrong
|
||||
form = JsonForm({'name': 'xy', 'jfield': '{"foo"}'})
|
||||
# Appears once in the textarea and once in the error message
|
||||
self.assertEqual(form.as_p().count(escape('{"foo"}')), 2)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue