1212
1313
1414class SimpleForm (BaseModel ):
15+ name : str
16+ size : int = 4
17+
18+
19+ class FormWithConstraints (BaseModel ):
1520 name : str = Field (..., max_length = 10 , min_length = 2 , description = 'This field is required, it must have length 2-10' )
1621 size : int = Field (4 , ge = 0 , le = 10 , multiple_of = 2 , description = 'size with range 0-10 and step with 2' )
1722
@@ -44,9 +49,6 @@ def test_simple_form_fields():
4449 'locked' : False ,
4550 'htmlType' : 'text' ,
4651 'type' : 'FormFieldInput' ,
47- 'maxLength' : 10 ,
48- 'minLength' : 2 ,
49- 'description' : 'This field is required, it must have length 2-10' ,
5052 },
5153 {
5254 'name' : 'size' ,
@@ -56,10 +58,6 @@ def test_simple_form_fields():
5658 'locked' : False ,
5759 'htmlType' : 'number' ,
5860 'type' : 'FormFieldInput' ,
59- 'ge' : 0 ,
60- 'le' : 10 ,
61- 'multipleOf' : 2 ,
62- 'description' : 'size with range 0-10 and step with 2' ,
6361 },
6462 ],
6563 }
@@ -96,6 +94,42 @@ def test_inline_form_fields():
9694 }
9795
9896
97+ def test_form_with_constraints_fields ():
98+ m = components .ModelForm [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+
99133async def test_simple_form_submit ():
100134 form_dep = fastui_form (SimpleForm )
101135
0 commit comments