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
[Bug?]: Weird validation issues when using nested checkboxes #310
Comments
You're right that this is because the checkboxes are not included in the For the lone Here's a stackblitz demoing this: https://stackblitz.com/edit/remix-run-remix-tvvemm?file=app%2Froutes%2F_index.tsx |
Hi @airjp73, first of all thanks for this beautiful project and also for such a quick reply. As for your first paragraph I got it, although I think it could be something to write about in the docs. As for the second paragraph, well, we'll add hidden fields alongside nested checkboxes to make things work without having to change schemas or doing extra work. I think the helper would be a good addition to Again IMHO, there is this gap, or mismatch, in the validation logic where // this fails to properly validate the form, but succeeds when validating the parsed output of a valid submission
const schema = z.object({
another: z.object({
checkbox: z.string({ required_error: 'Another checkbox is required' }),
}),
})
const validator = withZod(schema); // this succeeds when validating the form, but fails when validating the parsed JSON output of the validation itself
const schema = z.object({
'another.checkbox': z.string({ required_error: 'Another checkbox is required' }),
})
const validator = withZod(schema); What do you think? Is this a valid point? |
I think you've pretty accurately describe the challenge here, but I definitely think this is something that has to be solved by the schema itself. This issue is as much to do with the limitations of For example: I redid the reproduction using So I think a helper in const validator = withZod(
z.object({
// nested would be a thin wrapper around z.preprocess
another: zfd.nested({
checkbox: z.string() // or zfd.checkbox() if you want
})
})
); Since validators need to work on any On another note, if you don't want to use an extra helper, I would probably recommend removing the last layer of nesting instead of adding hidden fields. If the checkbox is the only field in the object, it doesn't seem necessary. |
Thanks again for your reply, I think your points are totally legit and it changed my view of the problem to match yours. The |
Sorry for the delayed response. I'm not super strict about needing to change this to a feature request. Would you have time to help with a PR to add in the helper? |
Which packages are impacted?
remix-validated-form
@remix-validated-form/with-zod
@remix-validated-form/with-yup
zod-form-data
What version of these packages are you using?
remix-validated-form
: 5.0.2@remix-validated-form/with-zod
: 2.0.6Please provide a link to a minimal reproduction of the issue.
https://stackblitz.com/edit/remix-run-remix-zthg3e
Steps to Reproduce the Bug or Issue
All fields shown are required, try to submit the form without filling any of them and look at the error messages.
From my understanding of how it should work the result is totally unexpected. Each field should show the error message defined in the schema but:
deeply.nested.checkbox
: shows "Required" instead of "Deep checkbox is required"nested.checkbox
: shows "Required" instead of "Nested checkbox is required" (the sibling text field works correctly)another.checkbox
: concurs to validation (non flagging it prevents the submission) but no errors are shownI think it is related to the fact that unchecked checkboxes don't show up in
FormData
and the issue cascades from that.Expected behavior
I expected to see the error messages defined in the Zod schema alongside each errored field.
Screenshots or Videos
Platform
Additional context
This issue also affects radios.
The text was updated successfully, but these errors were encountered: