12
12
13
13
14
14
class SimpleForm (BaseModel ):
15
+ name : str
16
+ size : int = 4
17
+
18
+
19
+ class FormWithConstraints (BaseModel ):
15
20
name : str = Field (..., max_length = 10 , min_length = 2 , description = 'This field is required, it must have length 2-10' )
16
21
size : int = Field (4 , ge = 0 , le = 10 , multiple_of = 2 , description = 'size with range 0-10 and step with 2' )
17
22
@@ -44,9 +49,6 @@ def test_simple_form_fields():
44
49
'locked' : False ,
45
50
'htmlType' : 'text' ,
46
51
'type' : 'FormFieldInput' ,
47
- 'maxLength' : 10 ,
48
- 'minLength' : 2 ,
49
- 'description' : 'This field is required, it must have length 2-10' ,
50
52
},
51
53
{
52
54
'name' : 'size' ,
@@ -56,10 +58,6 @@ def test_simple_form_fields():
56
58
'locked' : False ,
57
59
'htmlType' : 'number' ,
58
60
'type' : 'FormFieldInput' ,
59
- 'ge' : 0 ,
60
- 'le' : 10 ,
61
- 'multipleOf' : 2 ,
62
- 'description' : 'size with range 0-10 and step with 2' ,
63
61
},
64
62
],
65
63
}
@@ -96,6 +94,42 @@ def test_inline_form_fields():
96
94
}
97
95
98
96
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
+
99
133
async def test_simple_form_submit ():
100
134
form_dep = fastui_form (SimpleForm )
101
135
0 commit comments