-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwizard_form.py
More file actions
137 lines (124 loc) · 4.02 KB
/
wizard_form.py
File metadata and controls
137 lines (124 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
Ejemplo de formulario multi-paso (wizard) con FormStep.
Demuestra cómo crear un formulario wizard con validación por paso
y validación global.
"""
from codeforms import (
CheckboxField,
EmailField,
Form,
FormStep,
NumberField,
SelectField,
SelectOption,
TextField,
)
def create_registration_wizard() -> Form:
"""Crea un formulario wizard de registro de usuario en 3 pasos."""
return Form(
name="registration_wizard",
content=[
# Paso 1: Información personal
FormStep(
title="Personal Information",
description="Tell us about yourself",
content=[
TextField(name="first_name", label="First Name", required=True),
TextField(name="last_name", label="Last Name", required=True),
EmailField(name="email", label="Email", required=True),
],
),
# Paso 2: Preferencias
FormStep(
title="Preferences",
description="Choose your plan and preferences",
content=[
SelectField(
name="plan",
label="Plan",
required=True,
options=[
SelectOption(value="free", label="Free"),
SelectOption(value="pro", label="Professional"),
SelectOption(value="enterprise", label="Enterprise"),
],
),
NumberField(
name="team_size",
label="Team Size",
min_value=1,
max_value=1000,
),
],
),
# Paso 3: Confirmación
FormStep(
title="Confirmation",
description="Review and accept the terms",
content=[
CheckboxField(
name="terms",
label="I accept the terms and conditions",
required=True,
),
],
validation_mode="on_submit",
),
],
)
if __name__ == "__main__":
form = create_registration_wizard()
# Verificar estructura
print(f"Form: {form.name}")
print(f"Steps: {len(form.get_steps())}")
print(f"Total fields: {len(form.fields)}")
for i, step in enumerate(form.get_steps()):
print(f"\n Step {i + 1}: {step.title}")
for field in step.fields:
print(
f" - {field.name} ({'required' if field.required else 'optional'})"
)
# Validar paso 1
print("\n--- Validating Step 1 ---")
result = form.validate_step(
0,
{
"first_name": "John",
"last_name": "Doe",
"email": "john@example.com",
},
)
print(f"Step 1 valid: {result['success']}")
# Validar paso 2
print("\n--- Validating Step 2 ---")
result = form.validate_step(
1,
{
"plan": "pro",
"team_size": 5,
},
)
print(f"Step 2 valid: {result['success']}")
# Validar todos los pasos
print("\n--- Validating All Steps ---")
result = form.validate_all_steps(
{
"first_name": "John",
"last_name": "Doe",
"email": "john@example.com",
"plan": "pro",
"team_size": 5,
"terms": True,
}
)
print(f"All steps valid: {result['success']}")
# Exportar HTML
print("\n--- HTML Export ---")
export = form.export("html_bootstrap5")
print(export["output"][:300] + "...")
# JSON roundtrip
print("\n--- JSON Roundtrip ---")
json_str = form.model_dump_json()
restored = Form.model_validate_json(json_str)
print(f"Restored steps: {len(restored.get_steps())}")
print(f"Restored fields: {len(restored.fields)}")