-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_usage.py
More file actions
218 lines (199 loc) · 6.92 KB
/
Copy pathbasic_usage.py
File metadata and controls
218 lines (199 loc) · 6.92 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"""
Basic usage examples for codeforms.
Demonstrates form creation, data validation, JSON serialization,
and HTML export.
"""
from codeforms import (
CheckboxField,
CheckboxGroupField,
EmailField,
Form,
RadioField,
SelectField,
SelectOption,
TextField,
ValidationRule,
validate_form_data,
)
def create_registration_form() -> Form:
"""Registration form with multiple field types and validation rules."""
return Form(
name="registration_form",
fields=[
TextField(
name="username",
label="Username",
required=True,
minlength=3,
maxlength=50,
validation_rules=[
ValidationRule(
type="regex",
value="^[a-zA-Z0-9_]+$",
message="Only letters, numbers, and underscores are allowed",
)
],
),
EmailField(
name="email",
label="Email address",
required=True,
validation_rules=[
ValidationRule(
type="email",
value=True,
message="Must be a valid email address",
)
],
),
CheckboxGroupField(
name="notifications",
label="Notifications",
options=[
SelectOption(value="email", label="Email"),
SelectOption(value="sms", label="SMS"),
SelectOption(value="push", label="Push notifications"),
],
help_text="Select your notification preferences",
),
RadioField(
name="plan",
label="Subscription plan",
options=[
SelectOption(value="basic", label="Basic"),
SelectOption(value="pro", label="Professional"),
SelectOption(value="enterprise", label="Enterprise"),
],
required=True,
),
CheckboxField(
name="terms",
label="I accept the terms and conditions",
required=True,
validation_rules=[
ValidationRule(
type="required",
value=True,
message="You must accept the terms and conditions",
)
],
),
],
)
def create_product_form() -> Form:
"""Product form showcasing single and multi-select fields."""
return Form(
name="product_form",
fields=[
TextField(name="name", label="Product name", required=True),
# Single select
SelectField(
name="category",
label="Category",
required=True,
options=[
SelectOption(value="electronics", label="Electronics"),
SelectOption(value="clothing", label="Clothing"),
SelectOption(value="food", label="Food"),
],
),
# Multi-select with limits
SelectField(
name="tags",
label="Tags",
multiple=True,
min_selected=1,
max_selected=3,
options=[
SelectOption(value="new", label="New"),
SelectOption(value="sale", label="Sale"),
SelectOption(value="featured", label="Featured"),
SelectOption(value="limited", label="Limited edition"),
SelectOption(value="seasonal", label="Seasonal"),
],
help_text="Select between 1 and 3 tags",
),
# Multi-select without limits
SelectField(
name="colors",
label="Available colors",
multiple=True,
options=[
SelectOption(value="red", label="Red"),
SelectOption(value="blue", label="Blue"),
SelectOption(value="green", label="Green"),
SelectOption(value="black", label="Black"),
SelectOption(value="white", label="White"),
],
),
],
)
if __name__ == "__main__":
# --- Product form: validation ---
form = create_product_form()
valid_data = {
"name": "Product 1",
"category": "electronics",
"tags": ["new", "sale", "featured"], # 3 tags (max allowed)
"colors": ["red", "blue"],
}
invalid_data = {
"name": "Product 2",
"category": "invalid_category", # invalid option
"tags": ["new", "sale", "featured", "limited"], # exceeds max of 3
"colors": ["red", "invalid_color"], # invalid option
}
print("\n=== Validating valid data ===")
result = validate_form_data(form, valid_data)
if result["success"]:
print("Success:", result["message"])
print("Validated data:", result["data"])
else:
print("Error:", result["message"])
print("Errors:", result["errors"])
print("\n=== Validating invalid data ===")
result = validate_form_data(form, invalid_data)
if result["success"]:
print("Success:", result["message"])
print("Validated data:", result["data"])
else:
print("Error:", result["message"])
print("Errors:", result["errors"])
# --- JSON serialization roundtrip ---
print("\n=== JSON roundtrip ===")
form_json = form.model_dump_json()
print(form_json)
form_imported = Form.loads(form_json)
print(form_imported)
result = validate_form_data(form_imported, valid_data)
if result["success"]:
print("Success:", result["message"])
print("Validated data:", result["data"])
else:
print("Error:", result["message"])
print("Errors:", result["errors"])
# --- Contact form: default values and HTML export ---
print("\n=== Contact form with HTML export ===")
contact_form = Form(
name="contact_form",
fields=[
TextField(
name="name",
label="Name",
required=True,
minlength=3,
maxlength=50,
pattern="^[a-zA-Z ]+$",
),
EmailField(name="email", label="Email", required=True),
],
)
contact_form.set_default_values(
data={"name": "John Doe", "email": "john@example.com"}
)
# Export as Bootstrap 4 HTML
print(contact_form.export(output_format="html_bootstrap4", id="my_form"))
# Validate data
test_data = {"name": "John Doe", "email": "john@example.com"}
result = contact_form.validate_data(test_data)
print(result)