66from fastapi import HTTPException
77from fastui import components
88from fastui .forms import FormFile , Textarea , fastui_form
9- from pydantic import BaseModel
9+ from pydantic import BaseModel , Field
1010from starlette .datastructures import FormData , Headers , UploadFile
1111from typing_extensions import Annotated
1212
@@ -16,6 +16,11 @@ class SimpleForm(BaseModel):
1616 size : int = 4
1717
1818
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+
1924class FakeRequest :
2025 """
2126 TODO replace this with httpx or similar maybe, perhaps this is sufficient
@@ -89,6 +94,42 @@ def test_inline_form_fields():
8994 }
9095
9196
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+
92133async def test_simple_form_submit ():
93134 form_dep = fastui_form (SimpleForm )
94135
0 commit comments