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
Form starts as tainted with Zod enums and select elements #73
Comments
That's a good catch. As a workaround until I've examined this better, does this work? z.nativeEnum(State).default('' as State) |
Nice idea -- yes, that works. Thanks. |
Great! There's a note about this (ab)use of the type system here: https://superforms.vercel.app/default-values#changing-a-default-value |
Closing this, I think the workaround is the only thing to do if the general type safety of the library is to be kept. |
Note, it might be worth mentioning this issue and the workaround in the Tainted form check section of the docs, as it may not be obvious to users why they are getting the |
Yes, will add that to the docs. |
I have an address form with the
state
property of the Zod schema defined like:In the form, the
state
field is aselect
element with the first option defined as:The problem is that
superValidate
generatesundefined
as the value of$form.state
, but when bound to theselect
element, that immediately gets changed to the empty string, which marks the form as tainted. This means even without touching the form, if the user attempts to navigate away, they will see thetaintedMessage
alert.To avoid this problem, it would be helpful to be able to specify the default value for enums as being the empty string rather than
undefined
(possibly via a configuration option). For example, the following schema works as expected:Of course, it's not ideal to have to specify the schema that way, as it is not obvious why it is written that way, and technically it would allow an empty string to be submitted even though that value should not be allowed.
Would adding an option to handle this case be possible, or is there a better solution?
The text was updated successfully, but these errors were encountered: