Skip to content

Type conflicts in forms with file inputs #884

@blusrc

Description

@blusrc

I wanted to implement a form where users should submit the pdf file, but I keep getting the conflicts in Input props.

I tried to extend the InputProps in ui/input.tsx, but with no success

For now, I only found a solution that passes the pdf as z.any() and it passes as a string with fakepath to the values. I wanted to pass the File object to then use it on the backend

image

"use client";

import { zodResolver } from "@hookform/resolvers/zod";
import * as z from "zod";

import { Button } from "@/components/ui/button";
import {
  Form,
  FormControl,
  FormDescription,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { useForm } from "react-hook-form";

const MAX_SIZE_MB = 1;

const formSchema = z.object({
  pdf: z
    .custom<FileList>()
    .transform((file) => file.length > 0 && file.item(0))
    .refine(
      (file) => !file || (!!file && file.size <= MAX_SIZE_MB * 1024 * 1024),
      {
        message: `The profile picture must be a maximum of ${MAX_SIZE_MB}MB.`,
      }
    )
    .refine(
      (file) => !file || (!!file && file.type?.startsWith("application/pdf")),
      {
        message: "Only PDFs are allowed to be sent.",
      }
    ),
});

export function VerifyForm() {
  // 1. Define your form.
  const form = useForm<z.infer<typeof formSchema>>({
    resolver: zodResolver(formSchema),
    defaultValues: {
      pdf: null,
    },
  });

  // 2. Define a submit handler.
  function onSubmit(values: z.infer<typeof formSchema>) {
    // Do something with the form values.
    // ✅ This will be type-safe and validated.
    console.log(values);
  }

  return (
    <Form {...form}>
      <form
        onSubmit={form.handleSubmit(onSubmit)}
        className="space-y-8"
      >
        <FormField
          control={form.control}
          name="pdf"
          render={({ field }) => (
            <FormItem>
              <FormLabel>Student Card</FormLabel>
              <FormControl>
                <Input
                  type="file"
                  accept=".pdf"
                  placeholder="StudentCard.pdf"
                  {...field}
                />
              </FormControl>
              <FormDescription>
                This is used to get your University and Course year
              </FormDescription>
              <FormMessage />
            </FormItem>
          )}
        />
        <Button type="submit">Submit</Button>
      </form>
    </Form>
  );
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions