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

Unable to make checkbox required #42

Closed
jnoahjohnson opened this issue Jun 28, 2022 · 7 comments
Closed

Unable to make checkbox required #42

jnoahjohnson opened this issue Jun 28, 2022 · 7 comments

Comments

@jnoahjohnson
Copy link

I am trying to make a checkbox required to be true within the zod schema, but the form is still submitting no matter what I have tried so far. There is an example that shows this on the website, but it just sets the default to false. It has a required indicator, but accepts either true or false.

Is there a way to do this currently?

@LandSprutte
Copy link

LandSprutte commented Jun 29, 2022

if you want to make a "Terms of Service" kinda check:

tos: z.custom((data) => data === true, { message: "You have to accept the terms of service", }),

and then make a custom field in your form for the "tos"-field :)

@jnoahjohnson
Copy link
Author

Thank you!

@jnoahjohnson
Copy link
Author

jnoahjohnson commented Jun 30, 2022

@LandSprutte Okay, so I am actually running into an issue with this. It looks like when I create a custom input then the data is coming through as just empty strings. Any idea on this?

Screen Shot 2022-06-29 at 8 00 08 PM

Screen Shot 2022-06-29 at 8 00 17 PM

@jnoahjohnson jnoahjohnson reopened this Jun 30, 2022
@LandSprutte
Copy link

LandSprutte commented Jun 30, 2022

@jnoahjohnson try:

  tos: z.custom((data) => data, {
    message: "must accept TOS",
  }),

...... // other stuff

<Field name="tos" fieldType="boolean" type="checkbox" ....>
...
</Field>

You'll get the object { tos: "on" } 😉 and error message if not "checked"

@jnoahjohnson
Copy link
Author

Perfect, that worked. Thank you!

@felipefreitag
Copy link
Contributor

I'm closing this as completed since it seems the issue was resolved. Please feel free to reopen it if needed.

@mauscoelho
Copy link

Like this, you don't need to customise the Field

privacy: z.boolean().refine(value => value === true, { message: 'You must agree to the privacy statement' })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants