Skip to content

Commit 5aa61c9

Browse files
committed
Set form_name_root to unique values when creating editors
1 parent faa4f16 commit 5aa61c9

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Next version
55
~~~~~~~~~~~~
66

77
- Fixed the initialization of the JSON editor when we start with null values.
8+
- Changed the ID attributes of generated elements to hopefully be unique by
9+
using a unique ``form_name_root`` per editor instance.
810

911

1012
0.7 (2025-09-05)

django_json_schema_editor/static/django_json_schema_editor/widget.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ document.addEventListener("DOMContentLoaded", () => {
1919
})
2020
})
2121

22+
let editorIndex = 0
23+
2224
const initEditor = (el) => {
2325
if (el.dataset.foreignKey) {
2426
Object.assign(window.__djse_foreignKeys, JSON.parse(el.dataset.foreignKey))
@@ -32,6 +34,9 @@ const initEditor = (el) => {
3234
config.startval = value
3335
}
3436

37+
// Set an unique name so that form widgets get somewhat more unique names
38+
config.form_name_root = `djse${++editorIndex}`
39+
3540
const editor = new JSONEditor(el, config)
3641
editor.on("change", () => {
3742
input.value = JSON.stringify(editor.getValue())

tests/testapp/test_json_editor.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def test_json_editor_prose(page, live_server):
8383
expect(editor_container).to_be_visible()
8484

8585
# Check for the text field using the exact ID from the HTML
86-
text_input = page.locator('input[id="root[text]"]')
86+
text_input = page.locator('input[id*="[text]"]')
8787
expect(text_input).to_be_visible()
8888
expect(text_input).to_have_value("Hello World")
8989

@@ -118,7 +118,7 @@ def test_json_editor_edit_save(page, live_server):
118118
page.wait_for_timeout(1000) # Wait for editor to initialize
119119

120120
# Edit the regular text field
121-
text_input = page.locator('input[id="root[text]"]')
121+
text_input = page.locator('input[id*="[text]"]')
122122
expect(text_input).to_be_visible()
123123
text_input.fill("Updated text")
124124

@@ -140,7 +140,7 @@ def test_json_editor_edit_save(page, live_server):
140140
page.wait_for_timeout(1000)
141141

142142
# Verify text field was updated
143-
text_input = page.locator('input[id="root[text]"]')
143+
text_input = page.locator('input[id*="[text]"]')
144144
expect(text_input).to_have_value("Updated text")
145145

146146
# Verify prose field was updated
@@ -173,7 +173,7 @@ def test_json_editor_display(page, live_server):
173173
page.wait_for_timeout(1000)
174174

175175
# Verify that text field displays our test data
176-
text_input = page.locator('input[id="root[text]"]')
176+
text_input = page.locator('input[id*="[text]"]')
177177
expect(text_input).to_be_visible()
178178
expect(text_input).to_have_value("Test text field")
179179

@@ -205,7 +205,7 @@ def test_json_editor_ui_elements(page, live_server):
205205
expect(page.locator(".prose-menubar__button[title='underline']")).to_be_visible()
206206

207207
# Verify the text editor field is present
208-
text_input = page.locator('input[id="root[text]"]')
208+
text_input = page.locator('input[id*="[text]"]')
209209
expect(text_input).to_be_visible()
210210

211211
# Focus on the prose editor

0 commit comments

Comments
 (0)