6
6
from fastapi import HTTPException
7
7
from fastui import components
8
8
from fastui .forms import FormFile , Textarea , fastui_form
9
- from pydantic import BaseModel
9
+ from pydantic import BaseModel , Field
10
10
from starlette .datastructures import FormData , Headers , UploadFile
11
11
from typing_extensions import Annotated
12
12
@@ -16,6 +16,11 @@ class SimpleForm(BaseModel):
16
16
size : int = 4
17
17
18
18
19
+ class FormWithConstraints (BaseModel ):
20
+ name : str = Field (..., max_length = 10 , min_length = 2 , description = 'This field is required, it must have length 2-10' )
21
+ size : int = Field (4 , ge = 0 , le = 10 , multiple_of = 2 , description = 'size with range 0-10 and step with 2' )
22
+
23
+
19
24
class FakeRequest :
20
25
"""
21
26
TODO replace this with httpx or similar maybe, perhaps this is sufficient
@@ -89,6 +94,42 @@ def test_inline_form_fields():
89
94
}
90
95
91
96
97
+ def test_form_with_constraints_fields ():
98
+ m = components .ModelForm (model = FormWithConstraints , submit_url = '/foobar/' )
99
+
100
+ assert m .model_dump (by_alias = True , exclude_none = True ) == {
101
+ 'submitUrl' : '/foobar/' ,
102
+ 'method' : 'POST' ,
103
+ 'type' : 'ModelForm' ,
104
+ 'formFields' : [
105
+ {
106
+ 'name' : 'name' ,
107
+ 'title' : ['Name' ],
108
+ 'required' : True ,
109
+ 'locked' : False ,
110
+ 'htmlType' : 'text' ,
111
+ 'type' : 'FormFieldInput' ,
112
+ 'description' : 'This field is required, it must have length 2-10' ,
113
+ 'maxLength' : 10 ,
114
+ 'minLength' : 2 ,
115
+ },
116
+ {
117
+ 'name' : 'size' ,
118
+ 'title' : ['Size' ],
119
+ 'initial' : 4 ,
120
+ 'required' : False ,
121
+ 'locked' : False ,
122
+ 'htmlType' : 'number' ,
123
+ 'type' : 'FormFieldInput' ,
124
+ 'description' : 'size with range 0-10 and step with 2' ,
125
+ 'le' : 10 ,
126
+ 'ge' : 0 ,
127
+ 'multipleOf' : 2 ,
128
+ },
129
+ ],
130
+ }
131
+
132
+
92
133
async def test_simple_form_submit ():
93
134
form_dep = fastui_form (SimpleForm )
94
135
0 commit comments