Skip to content

Commit 2823c04

Browse files
committed
Add more form constraints
1 parent acc26f0 commit 2823c04

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

demo/forms.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ class SizeModel(BaseModel):
141141

142142
class BigModel(BaseModel):
143143
name: str | None = Field(
144-
None, description='This field is not required, it must start with a capital letter if provided'
144+
None,
145+
max_length=10,
146+
min_length=2,
147+
description='This field is not required, it must start with a capital letter if provided',
145148
)
146149
profile_pic: Annotated[UploadFile, FormFile(accept='image/*', max_size=16_000)] = Field(
147150
description='Upload a profile picture, must not be more than 16kb'
@@ -153,6 +156,7 @@ class BigModel(BaseModel):
153156
human: bool | None = Field(
154157
None, title='Is human', description='Are you human?', json_schema_extra={'mode': 'switch'}
155158
)
159+
number: int | None = Field(None, title='Number', ge=0, le=10, multiple_of=2, description='This is a number')
156160
size: SizeModel
157161

158162
@field_validator('name')

src/npm-fastui/src/components/FormField.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@ interface FormFieldInputProps extends BaseFormFieldProps {
3030
type: 'FormFieldInput'
3131
htmlType: 'text' | 'date' | 'datetime-local' | 'time' | 'email' | 'url' | 'number' | 'password'
3232
initial?: string | number
33+
maxLength?: number
34+
minLength?: number
35+
ge?: number
36+
le?: number
37+
multipleOf?: number
3338
placeholder?: string
3439
}
3540

3641
export const FormFieldInputComp: FC<FormFieldInputProps> = (props) => {
37-
const { name, placeholder, required, htmlType, locked } = props
42+
const { name, placeholder, required, htmlType, locked, maxLength, minLength, ge, le, multipleOf } = props
3843

3944
return (
4045
<div className={useClassName(props)}>
@@ -49,6 +54,11 @@ export const FormFieldInputComp: FC<FormFieldInputProps> = (props) => {
4954
disabled={locked}
5055
placeholder={placeholder}
5156
aria-describedby={descId(props)}
57+
maxLength={maxLength}
58+
minLength={minLength}
59+
min={ge}
60+
max={le}
61+
step={multipleOf}
5262
/>
5363
<ErrorDescription {...props} />
5464
</div>

src/python-fastui/fastui/components/forms.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class FormFieldInput(BaseFormField):
3232
html_type: InputHtmlType = pydantic.Field(default='text', serialization_alias='htmlType')
3333
initial: _t.Union[str, float, None] = None
3434
placeholder: _t.Union[str, None] = None
35+
max_length: _t.Union[int, None] = pydantic.Field(default=None, serialization_alias='maxLength')
36+
min_length: _t.Union[int, None] = pydantic.Field(default=None, serialization_alias='minLength')
37+
ge: _t.Union[int, float, None] = None
38+
le: _t.Union[int, float, None] = None
39+
multiple_of: _t.Union[int, float, None] = pydantic.Field(default=None, serialization_alias='multipleOf')
3540
type: _t.Literal['FormFieldInput'] = 'FormFieldInput'
3641

3742

src/python-fastui/fastui/json_schema.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ def json_schema_field_to_field(
186186
required=required,
187187
initial=schema.get('default'),
188188
description=schema.get('description'),
189+
max_length=schema.get('maxLength'),
190+
min_length=schema.get('minLength'),
191+
ge=schema.get('minimum'),
192+
le=schema.get('maximum'),
193+
multiple_of=schema.get('multipleOf'),
189194
)
190195

191196

0 commit comments

Comments
 (0)