Skip to content

Commit 54e44f4

Browse files
committed
Fix Django forms (convert new ReactPy Event type to a plain dict)
1 parent 106513e commit 54e44f4

File tree

2 files changed

+27
-35
lines changed

2 files changed

+27
-35
lines changed

src/js/src/components.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,10 @@ export class DjangoForm extends React.Component<DjangoFormProps> {
1010
event.preventDefault();
1111
const formData = new FormData(form);
1212

13-
// Convert the FormData object to a plain object by iterating through it
14-
// If duplicate keys are present, convert the value into an array of values
15-
const entries = formData.entries();
16-
const formDataArray = Array.from(entries);
17-
const formDataObject = formDataArray.reduce<Record<string, unknown>>(
18-
(acc, [key, value]) => {
19-
if (acc[key]) {
20-
if (Array.isArray(acc[key])) {
21-
acc[key].push(value);
22-
} else {
23-
acc[key] = [acc[key], value];
24-
}
25-
} else {
26-
acc[key] = value;
27-
}
28-
return acc;
29-
},
30-
{},
31-
);
13+
// Convert the FormData object to a plain object
14+
const formObject = Object.fromEntries(formData.entries());
3215

33-
onSubmitCallback(formDataObject);
16+
onSubmitCallback(formObject);
3417
};
3518

3619
if (form) {

src/reactpy_django/forms/components.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from logging import getLogger
34
from pathlib import Path
45
from typing import TYPE_CHECKING, Any, Callable, Union, cast
56
from uuid import uuid4
@@ -26,6 +27,7 @@
2627

2728
from reactpy.types import VdomDict
2829

30+
_logger = getLogger(__name__)
2931
DjangoForm = component_from_file(
3032
Path(__file__).parent.parent / "static" / "reactpy_django" / "index.js",
3133
import_names=("DjangoForm"),
@@ -67,27 +69,34 @@ def _django_form(
6769
@hooks.use_async_effect(dependencies=[str(submitted_data)])
6870
async def render_form():
6971
"""Forms must be rendered in an async loop to allow database fields to execute."""
70-
if submitted_data:
71-
await ensure_async(initialized_form.full_clean, thread_sensitive=thread_sensitive)()
72-
success = not initialized_form.errors.as_data()
73-
if success and on_success:
74-
await ensure_async(on_success, thread_sensitive=thread_sensitive)(form_event)
75-
if not success and on_error:
76-
await ensure_async(on_error, thread_sensitive=thread_sensitive)(form_event)
77-
if success and auto_save and isinstance(initialized_form, ModelForm):
78-
await ensure_async(initialized_form.save)()
79-
set_submitted_data(None)
80-
81-
set_rendered_form(
82-
await ensure_async(initialized_form.render)(form_template or config.REACTPY_DEFAULT_FORM_TEMPLATE)
83-
)
72+
73+
_logger.debug(f"Rendering form with submitted data: {submitted_data}")
74+
try:
75+
if submitted_data:
76+
await ensure_async(initialized_form.full_clean, thread_sensitive=thread_sensitive)()
77+
success = not initialized_form.errors.as_data()
78+
if success and on_success:
79+
await ensure_async(on_success, thread_sensitive=thread_sensitive)(form_event)
80+
if not success and on_error:
81+
await ensure_async(on_error, thread_sensitive=thread_sensitive)(form_event)
82+
if success and auto_save and isinstance(initialized_form, ModelForm):
83+
await ensure_async(initialized_form.save)()
84+
set_submitted_data(None)
85+
86+
set_rendered_form(
87+
await ensure_async(initialized_form.render)(form_template or config.REACTPY_DEFAULT_FORM_TEMPLATE)
88+
)
89+
except Exception:
90+
_logger.exception("Error during form processing")
91+
8492

8593
async def on_submit_callback(new_data: dict[str, Any]):
8694
"""Callback function provided directly to the client side listener. This is responsible for transmitting
8795
the submitted form data to the server for processing."""
8896
# The client side listener passes a ReactPy Event object which needs to be
8997
# converted to a standard dictionary.
90-
convert_form_fields(dict(new_data), initialized_form)
98+
new_data = dict(new_data)
99+
convert_form_fields(new_data, initialized_form)
91100

92101
if on_receive_data:
93102
new_form_event = FormEventData(

0 commit comments

Comments
 (0)