Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boolean value in update form is always required #277

Open
ManiMozaffar opened this issue Apr 19, 2024 · 3 comments · May be fixed by #323
Open

Boolean value in update form is always required #277

ManiMozaffar opened this issue Apr 19, 2024 · 3 comments · May be fixed by #323
Labels
bug Something isn't working
Milestone

Comments

@ManiMozaffar
Copy link
Contributor

ManiMozaffar commented Apr 19, 2024

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 ManiMozaffar changed the title Boolean value in form is always required Boolean value in filter form is always required Apr 19, 2024
@ManiMozaffar ManiMozaffar changed the title Boolean value in filter form is always required Boolean value in update form is always required Apr 19, 2024
@sydney-runkle sydney-runkle added this to the v2.6.0 milestone Apr 19, 2024
@sydney-runkle sydney-runkle added the bug Something isn't working label May 2, 2024
@sydney-runkle
Copy link
Member

@ManiMozaffar,

Thanks for the report. Looks like a bug indeed!

@sydney-runkle sydney-runkle modified the milestones: v0.6.0, v0.7.0 May 2, 2024
@sergue1 sergue1 linked a pull request May 28, 2024 that will close this issue
@samuelcolvin
Copy link
Member

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

@sergue1
Copy link
Contributor

sergue1 commented May 31, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants