Replies: 6 comments 3 replies
-
What issues are you seeing when using |
Beta Was this translation helpful? Give feedback.
-
I have to set all the defaultValue properties to the exact form schema: const form = useForm<FormSchema, ValibotValidator>({
defaultValues: { // Cant do this, I am required to set description + someComplexObject
name: "",
},
onSubmit: async ({ value }) => {
},
validatorAdapter: valibotValidator(),
}); error:
|
Beta Was this translation helpful? Give feedback.
-
I created a stackblitz example to show the exact replication of the issue I am facing @crutchcorn |
Beta Was this translation helpful? Give feedback.
-
I'm running into this same issue today. I'd like to be able to initialize optional fields as const form = useAppForm({
defaultValues: {
title: '',
description: undefined,
},
validators: {
onSubmit: z.object({ // Type 'string' is not assignable to type 'undefined'.ts(2322)
title: z.string().nonempty('Required'),
description: z.string().optional(),
}),
},
onSubmit: async ({ value }) => console.log(value),
}); but when doing so I get a I suppose this may just be a limitation of controlled inputs as mentioned in the React docs but maybe TanStack Form can find an elegant solution around this. Otherwise, I'm left having to manually go through my form output object and check which fields are optional and set them to |
Beta Was this translation helpful? Give feedback.
-
I have this zod schema and this is the form
|
Beta Was this translation helpful? Give feedback.
-
I think what people are looking for is a different input type and output type. const courseFormSchema = z.object({
course: z.object({
title: z.string().min(1, "Course title is required"),
description: z.string().min(1, "Course description is required"),
categoryId: z.string().min(1, "Category is required"),
skillLevel: z.enum(["beginner", "intermediate", "advanced", "all"]),
instructorId: z.string().min(1, "Instructor is required"),
imageUrl: z.string().optional(),
status: z.enum(["draft", "published"]).default("draft"),
}),
tags: z.array(z.string()).catch([]),
prerequisites: z.array(z.string()).catch([]),
}); type CourseFormSchemaInput = z.input<typeof courseFormSchema>;
type CourseFormSchemaInput = {
course: {
title: string;
description: string;
categoryId: string;
instructorId: string;
skillLevel: "beginner" | "intermediate" | "advanced" | "all";
status?: "draft" | "published" | undefined;
imageUrl?: string | undefined;
};
prerequisites?: unknown;
tags?: unknown;
}
type CourseFormSchemaOutput = z.output<typeof courseFormSchema>;
type CourseFormSchemaOutput = {
course: {
status: "draft" | "published";
title: string;
description: string;
categoryId: string;
skillLevel: "beginner" | "intermediate" | "advanced" | "all";
instructorId: string;
imageUrl?: string | undefined;
};
tags: string[];
prerequisites: string[];
} In my form i would then use the input type as my cast const form = useAppForm({
defaultValues: {
course: {
title: "",
description: "",
categoryId: "",
skillLevel: "all",
instructorId: "",
imageUrl: undefined,
status: undefined,
},
tags: [],
prerequisites: [],
} as CourseFormSchemaInput, // use input type here
validators: {
onSubmit: courseFormSchema,
},
onSubmit: async ({ value }) => {
// Now the problem is I used the corrrect type for my default values.
// But now my value object here is the input type not the output type from zod
const validatedValue = courseFormSchema.parse(value);
// This is what I have decided to do for now. Which is an extra unnecessary call but it fixes the types.
// I thought I could just do onSubmit: async ({ value } : {value CourseFormSchemaOutput ) => {
// But that causes type errors above
await createCourseMutation.mutateAsync(validatedValue, {
onSuccess: (data) => {
toast.success("Course created successfully");
navigate({
to: "/admin/courses/$courseId/content",
params: { courseId: data.courseId },
});
},
onError: () => {
toast.error("Failed to create course");
},
});
},
}); |
Beta Was this translation helpful? Give feedback.
-
Currently its not possible to set certain defaultvalues to undefined if we use a validator like Valibot. In the example shown below, we have a schema where we have a complex object that has many properties and nested values
Would be even nicer to only select the defaultValues you want, something like:
Beta Was this translation helpful? Give feedback.
All reactions