Skip to content

Commit 16f076d

Browse files
committed
validate_form_args function
1 parent 5d2dd0e commit 16f076d

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/reactpy_django/forms/components.py

+2-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
set_value_prop_on_select_element,
1818
transform_value_prop_on_input_element,
1919
)
20-
from reactpy_django.forms.utils import convert_form_fields
20+
from reactpy_django.forms.utils import convert_form_fields, validate_form_args
2121
from reactpy_django.types import AsyncFormEvent, FormEventData, SyncFormEvent
2222
from reactpy_django.utils import ensure_async
2323

@@ -56,18 +56,8 @@ def _django_form(
5656
rendered_form, set_rendered_form = hooks.use_state(cast(Union[str, None], None))
5757
uuid = uuid_ref.current
5858

59-
# Validate the provided arguments
60-
if len(top_children) != top_children_count.current or len(bottom_children) != bottom_children_count.current:
61-
msg = "Dynamically changing the number of top or bottom children is not allowed."
62-
raise ValueError(msg)
63-
if not isinstance(form, (type(Form), type(ModelForm))):
64-
msg = (
65-
"The provided form must be an uninitialized Django Form. "
66-
"Do NOT initialize your form by calling it (ex. `MyForm()`)."
67-
)
68-
raise TypeError(msg)
69-
7059
# Initialize the form with the provided data
60+
validate_form_args(top_children, top_children_count, bottom_children, bottom_children_count, form)
7161
initialized_form = form(data=submitted_data)
7262
form_event = FormEventData(
7363
form=initialized_form, submitted_data=submitted_data or {}, set_submitted_data=set_submitted_data

src/reactpy_django/forms/utils.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from __future__ import annotations
22

3-
from typing import Any
3+
from typing import Any, Sequence
44

55
from django.forms import BooleanField, Form, ModelForm, ModelMultipleChoiceField, MultipleChoiceField, NullBooleanField
6+
from reactpy import Ref
67

78

89
def convert_form_fields(data: dict[str, Any], initialized_form: Form | ModelForm) -> None:
@@ -14,3 +15,22 @@ def convert_form_fields(data: dict[str, Any], initialized_form: Form | ModelForm
1415

1516
elif isinstance(field, BooleanField) and not isinstance(field, NullBooleanField):
1617
data[field_name] = field_name in data
18+
19+
20+
def validate_form_args(
21+
top_children: Sequence,
22+
top_children_count: Ref[int],
23+
bottom_children: Sequence,
24+
bottom_children_count: Ref[int],
25+
form: type[Form] | type[ModelForm],
26+
) -> None:
27+
# Validate the provided arguments
28+
if len(top_children) != top_children_count.current or len(bottom_children) != bottom_children_count.current:
29+
msg = "Dynamically changing the number of top or bottom children is not allowed."
30+
raise ValueError(msg)
31+
if not isinstance(form, (type(Form), type(ModelForm))):
32+
msg = (
33+
"The provided form must be an uninitialized Django Form. "
34+
"Do NOT initialize your form by calling it (ex. `MyForm()`)."
35+
)
36+
raise TypeError(msg)

0 commit comments

Comments
 (0)