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
feature: add formData
schema
#81
Comments
I think the idea is cool, but I don't know yet how much sense it makes, because as far as I know |
Here is a code example: import { email, minLength, object, parse, string } from "valibot"; // 0.75 kB
import { loginUser } from "~/api";
const LoginSchema = object({
email: string([
minLength(1, "Please enter your email."),
email("The email address is badly formatted."),
]),
password: string([
minLength(1, "Please enter your password."),
minLength(8, "You password must have 8 characters or more."),
]),
});
export default function LoginRoute() {
async function login(formData: FormData) {
"use server";
try {
const { email, password } = parse(
LoginSchema,
Object.fromEntries(formData.entries())
);
await loginUser({ email, password });
} catch (error) {
// Handle errors
}
}
return (
<form action={login}>
<input name="email" type="email" required />
<input name="password" type="password" required minLength={8} />
<button type="submit">Login</button>
</form>
);
} |
Yes, in this case it's sufficient, but when we need to upload files, we have to encode the file in |
It is not a good idea to convert a file to base64. Instead the native functionality of the In fact, I have already implemented such a function for Modular Forms that can handle even complex objects and arrays: https://github.com/fabian-hiller/modular-forms/blob/main/packages/qwik/src/utils/getFormDataValues.ts |
Yeah I know that it's not a good idea to convert file to base64 but tRPC did not accept FormData :/ Thanks for your reply, I'm closing this ticket. |
I was not aware of that. If there is no other way, I understand the approach. Because of the modularity of Valibot, this functionality can be added quite easily. If someone starts a project for this that builds on Valibot, I'll be happy to link it on our ecosystem page. |
Hi,
I don't know how this would affect bundle size, but I think it would be interesting to have an API like :
It could be useful in
server actions
(Next.js) and client-side validations in<form>
.The text was updated successfully, but these errors were encountered: