Comments (6)
Definitely agree that the type coercion behaviour should be documented. Would you be interested in contributing? I think we can have a dedicated guide about type coercion.
FYI, to set a default value with zod, you should use .transform(v => v ?? false)
. .refine()
is meant for validation and Invalid input
is the default error message.
With regards to the behaviour, what you found is pretty much correct. This is mainly because FormData
express everything in the form of value
. There is no false
in the FormData
and all we know is if an entry with a specific name and value exists. The server cannot tell whether the checkbox is unchecked or if it is never rendered in the form. Conform also make an additional assumption that you haven't customize the checkbox value
. As it considers the result true
only when the value is on
(The default value of checkbox / radio button).
I am happy to adjust the behaviour if we believe there is a better approach. Here is a few concerns I had if we default the value to false
:
- We need to use
.refine()
to mark a checkbox as required (e.g.z.boolean().refine(value => value, 'Required')
) - There is no different with
z.boolean().optional()
anymore as there will always be a value. - Zod has a feature called
errorMap
which you can map both standard and custom error using a specific code. After this change, boolean no longer works with the standard required code as it is now a custom validation (i.e..refine()
)
from conform.
My bad with the refine
/transform
. I think more/better documentation is the way to go here. Having even more special behaviour makes things more unpredictable.
Maybe this could just be a short chapter somewhere? Something like the following:
Working with checkboxes
Checkboxes only have an "on" state. Leaving them unchecked will result in the browser not sending any value for that field.
To work around this, you can adjust your zod schema based on the prefered outcome:
z.boolean()
– Checkbox is required. Leaving it unchecked results in a validation errorz.boolean().optional()
– Checkbox is not required. Leaving it unchecked results inundefined
z.boolean().default(false)
– Checkbox is not required. Leaving it unchecked results infalse
.
from conform.
Actually forgot about default: z.boolean().default(false)
that should be the cleanest solution for my problem.
from conform.
Actually forgot about default:
z.boolean().default(false)
that should be the cleanest solution for my problem.
I don't know what the deal is, but this doesn't work for me. When it is unchecked it indeed defaults to false. But when it is checked I get Expected boolean, received string. The only way I can get checkboxes to consistently work correctly is z.preprocess((x) => x === "on", z.boolean())
from conform.
I don't know what the deal is, but this doesn't work for me. When it is unchecked it indeed defaults to false. But when it is checked I get Expected boolean, received string. The only way I can get checkboxes to consistently work correctly is
z.preprocess((x) => x === "on", z.boolean())
There is a test dedicated for this. So I would expect it to work. It could be a regression on zod 3.22. Maybe try downgrading your zod version to 3.21.4 and see if the issue is resolved.
from conform.
Related Issues (20)
- Error from lastSubmission no displayed when field is using useInputEvent HOT 3
- Conditionally rendered fields break onInput revalidation HOT 4
- "required" key is missing / wrong for fieldsets nested inside fieldlists HOT 2
- Non-default radio inputs get desynced after form reset HOT 1
- Installation instructions should mention `@conform-to/dom` HOT 3
- Missing documentation for description(Id) and attributes HOT 1
- Redirecting from action doesn't reset input value or error HOT 1
- Radix UI specific inputs HOT 1
- `getFieldsetConstraint` does not parse attributes correctly HOT 3
- Double submit with requestIntent HOT 2
- useFieldList does not reupdate HOT 4
- Allow keeping reference to removed inputs HOT 2
- Whitespace chars in number fields are incorrectly coerced to 0 with zod
- conform's automatic type coercion doesn't work with `z.discriminatedUnion` HOT 2
- Default Value with booleans + checkboxes doesnt work HOT 1
- Field error not sync between onValidate() method and UI HOT 4
- Controlled select component does not work when shouldValidate is onInput HOT 2
- onSubmit triggers multiple times. HOT 3
- Support submitting empty strings with Zod HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from conform.