FastUI icon indicating copy to clipboard operation
FastUI copied to clipboard

Boolean value in update form is always required

Open ManiMozaffar opened this issue 1 year ago • 3 comments

class UpdateForm(BaseModel):
    is_foo: bool

Renders to a required checkbox, so it always has to be "true" on submission! Screenshot 2024-04-19 at 11 18 30

workaround for now is to consider None as False. (so it makes checkbox optional), but that'd be cool to see this fixed :)

BoolUpdate = Annotated[bool | None, AfterValidator(lambda v: False if v is None else v)]
class UpdateForm(BaseModel): 
    is_foo: BoolUpdate = False

ManiMozaffar avatar Apr 19 '24 09:04 ManiMozaffar

@ManiMozaffar,

Thanks for the report. Looks like a bug indeed!

sydney-runkle avatar May 02 '24 00:05 sydney-runkle

Yup, this is a bug, the work around I've used is to implement a "yes, no" select, but that's very annoying.

samuelcolvin avatar May 30 '24 13:05 samuelcolvin

Btw since Pydantic schema generator does not mark fields with default or default_factory as required, there seems to be a simple workaround for the issue:

class FormFieldsBool(BaseModel):
    is_foo: bool = False

No need for custom type adapter, just initialize the field. This way behavior would be consistent across all field types.

sergue1 avatar May 31 '24 13:05 sergue1