|
28 | 28 | Hidden, |
29 | 29 | Choice, |
30 | 30 | choice_list, |
31 | | - unique_conlist |
| 31 | + unique_conlist, |
32 | 32 | ) |
33 | 33 |
|
34 | 34 | # Choice, |
@@ -79,9 +79,14 @@ def example_backend_validation(val: int) -> bool: |
79 | 79 |
|
80 | 80 |
|
81 | 81 | NumberExample = Annotated[ |
82 | | - int, Ge(1), Le(10), MultipleOf(multiple_of=3), Predicate(example_backend_validation) |
| 82 | + int, |
| 83 | + Ge(18), |
| 84 | + Le(99), |
| 85 | + MultipleOf(multiple_of=3), |
| 86 | + Predicate(example_backend_validation), |
83 | 87 | ] |
84 | 88 |
|
| 89 | + |
85 | 90 | class DropdownChoices(Choice): |
86 | 91 | _1 = ("1", "Option 1") |
87 | 92 | _2 = ("2", "Option 2") |
@@ -112,83 +117,118 @@ class ListChoices(Choice): |
112 | 117 | _6 = ("6", "Option 6") |
113 | 118 |
|
114 | 119 |
|
115 | | -class Education(BaseModel): |
116 | | - degree: str | None |
117 | | - year: int | None |
| 120 | +TestString = Annotated[str, Field(min_length=2, max_length=10)] |
118 | 121 |
|
119 | 122 |
|
120 | | -class Person(BaseModel): |
121 | | - name: str |
122 | | - age: Annotated[int, Ge(18), Le(99)] |
123 | | - education: Education |
| 123 | +class Education(BaseModel): |
| 124 | + degree: str |
| 125 | + years: int | None |
| 126 | + |
124 | 127 |
|
125 | 128 | def example_list_validation(val: int) -> bool: |
126 | 129 | return True |
127 | 130 |
|
128 | | -TestList = Annotated[ |
129 | | - unique_conlist(str, min_items=2, max_items=5), Predicate(example_backend_validation) |
130 | | -] |
131 | 131 |
|
132 | 132 | TestExampleNumberList = Annotated[ |
133 | | - unique_conlist(NumberExample, min_items=2, max_items=5), Predicate(example_list_validation) |
| 133 | + unique_conlist(NumberExample, min_items=2, max_items=5), |
| 134 | + Predicate(example_list_validation), |
134 | 135 | ] |
135 | 136 |
|
| 137 | + |
| 138 | +class Education2(BaseModel): |
| 139 | + degree: str |
| 140 | + years: int | None |
| 141 | + options: ListChoices |
| 142 | + languages: TestExampleNumberList |
| 143 | + |
| 144 | + |
| 145 | +class Person(BaseModel): |
| 146 | + name: str |
| 147 | + age: Annotated[int, Ge(18), Le(99), MultipleOf(multiple_of=3)] |
| 148 | + education: Education |
| 149 | + |
| 150 | + |
| 151 | +class Person2(BaseModel): |
| 152 | + name: str |
| 153 | + age: Annotated[int, Ge(18), Le(99), MultipleOf(multiple_of=3)] |
| 154 | + education: Education2 |
| 155 | + |
| 156 | + |
136 | 157 | TestPersonList = Annotated[ |
137 | 158 | unique_conlist(Person, min_items=2, max_items=5), Predicate(example_list_validation) |
138 | 159 | ] |
139 | 160 |
|
| 161 | + |
| 162 | +def is_maurits(val: str) -> bool: |
| 163 | + if val != "Maurits": |
| 164 | + raise ValueError("Has to be Maurits!") |
| 165 | + return True |
| 166 | + |
| 167 | + |
| 168 | +StringExample = Annotated[str, Predicate(is_maurits)] |
| 169 | + |
| 170 | + |
140 | 171 | @app.post("/form") |
141 | 172 | async def form(form_data: list[dict] = []): |
142 | 173 | def form_generator(state: State): |
143 | 174 | class TestForm0(FormPage): |
144 | | - model_config = ConfigDict(title="Form Title Page 0") |
| 175 | + model_config = ConfigDict(title="Form Title Page 1") |
145 | 176 |
|
146 | | - numberList: TestExampleNumberList |
147 | | - # personList: TestPersonList = [] |
| 177 | + number: NumberExample |
| 178 | + list: TestExampleNumberList |
| 179 | + # list_list: unique_conlist(TestExampleNumberList, min_items=1, max_items=5) |
| 180 | + # list_list_list: unique_conlist( |
| 181 | + # unique_conlist(Person2, min_items=1, max_items=5), |
| 182 | + # min_items=1, |
| 183 | + # max_items=2, |
| 184 | + # ) = [1, 2] |
| 185 | + test: TestString |
| 186 | + textList: unique_conlist(TestString, min_items=1, max_items=5) |
| 187 | + # numberList: TestExampleNumberList = [1, 2] |
| 188 | + person: Person2 |
| 189 | + personList: unique_conlist(Person2, min_items=2, max_items=5) |
148 | 190 | # ingleNumber: NumberExample |
149 | | - |
150 | 191 | # number0: Annotated[int, Ge(18), Le(99)] = 17 |
151 | 192 |
|
152 | 193 | form_data_0 = yield TestForm0 |
153 | 194 |
|
154 | 195 | class TestForm1(FormPage): |
155 | 196 | model_config = ConfigDict(title="Form Title Page 1") |
156 | 197 |
|
157 | | - number1: Annotated[int, Ge(18), Le(99)] = 17 |
| 198 | + contact_name2: StringExample |
| 199 | + options: ListChoices |
158 | 200 |
|
159 | 201 | form_data_1 = yield TestForm1 |
160 | 202 |
|
161 | 203 | class TestForm2(FormPage): |
162 | 204 | model_config = ConfigDict(title="Form Title Page 2") |
163 | 205 |
|
164 | | - number: NumberExample = 3 |
165 | | - text: Annotated[str, Field(min_length=3, max_length=12)] = "Default text" |
166 | | - textArea: LongText = "Text area default" |
167 | | - divider: Divider |
168 | | - label: Label = "Label" |
169 | | - hidden: Hidden = "Hidden" |
170 | | - # When there are > 3 choices a dropdown will be rendered |
171 | | - dropdown: DropdownChoices = "2" |
172 | | - # When there are <= 3 choices a radio group will be rendered |
173 | | - radio: RadioChoices = "3" |
174 | | - # checkbox: bool = True TODO: Fix validation errors on this |
| 206 | + contact_name3: StringExample |
| 207 | + age: NumberExample |
175 | 208 |
|
176 | | - # When there are <= 5 choices in a list a set of checkboxes are rendered |
177 | | - # multicheckbox: choice_list(MultiCheckBoxChoices, min_items=3) = ["1", "2"] |
178 | | - # list: choice_list(ListChoices) = [0, 1] |
| 209 | + form_data_2 = yield TestForm2 |
179 | 210 |
|
180 | | - person: Person |
| 211 | + class TestForm3(FormPage): |
| 212 | + model_config = ConfigDict(title="Form Title Page 3") |
181 | 213 |
|
182 | | - form_data_2 = yield TestForm2 |
| 214 | + contact_person: Person |
| 215 | + |
| 216 | + form_data_3 = yield TestForm3 |
183 | 217 |
|
184 | | - class TestSubmitForm(SubmitFormPage): |
185 | | - model_config = ConfigDict(title="Submit Form") |
| 218 | + class TestForm5(FormPage): |
| 219 | + model_config = ConfigDict(title="Form Title Page 4") |
186 | 220 |
|
187 | | - name_2: str | None = None |
| 221 | + contact_person_list: TestPersonList |
188 | 222 |
|
189 | | - form_data_submit = yield TestSubmitForm |
| 223 | + form_data_5 = yield TestForm5 |
190 | 224 |
|
191 | | - return form_data_0.model_dump() | form_data_1.model_dump() | form_data_2.model_dump() | form_data_submit.model_dump() |
| 225 | + return ( |
| 226 | + form_data_0.model_dump() |
| 227 | + | form_data_1.model_dump() |
| 228 | + | form_data_2.model_dump() |
| 229 | + | form_data_3.model_dump() |
| 230 | + | form_data_5.model_dump() |
| 231 | + ) |
192 | 232 |
|
193 | 233 | post_form(form_generator, state={}, user_inputs=form_data) |
194 | 234 | return "OK!" |
0 commit comments