ui icon indicating copy to clipboard operation
ui copied to clipboard

Input Number area shows red color and not able to submit a Form

Open dwk601 opened this issue 1 year ago • 1 comments

const schema = z.object({
  name: z.string().min(2).max(40),
  mainPosition: z.string(),
  mainRating: z
    .number({
      required_error: "Please enter a rating between 1-10",
      invalid_type_error: "Please enter a valid number",
    })
    .positive(),
  subPosition: z.string(),
  subRating: z
    .number({
      required_error: "Please enter a rating between 1-10",
      invalid_type_error: "Please enter a valid number",
    })
    .positive(),
  approved: z.boolean(),
});

export default function SetupPage() {
  const form = useForm<z.infer<typeof schema>>({
    resolver: zodResolver(schema),
    defaultValues: {
      name: "",
      mainPosition: "",
      subPosition: "",
      approved: false,
    },
  });

  function onsubmit(data: z.infer<typeof schema>) {
    toast({
      title: "You submitted the form",
      description: (
        <pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
          <code className="text-white">{JSON.stringify(data, null, 2)}</code>
        </pre>
      ),
    });
  }
<FormField
          control={form.control}
          name="mainRating"
          render={({ field }) => (
            <FormItem>
              <FormLabel>Main Rating</FormLabel>
              <FormControl>
                <Input placeholder="1-10" {...field} />
              </FormControl>
            </FormItem>
          )}
        />

Every other field than main rating, and sub rating are seems working without any issue but the number area keep denying the form to be submitted.

dwk601 avatar Feb 11 '24 20:02 dwk601

Hey, on the first look it seems like there's an issue with your form submission being prevented due to validation errors. But it's hard to say without more context.

When you encounter red validation indicating that the input is invalid, it's advisable to double-check your Zod validation rules and ensure they match the intended criteria.

Here are some steps that might be helpful:

  1. Zod Validation Rules: Make sure your Zod validation rules for the fields are correctly defined.

  2. React Hook State: Double-check the actual value stored in the React hook state for these fields. You can check with the getValues() or watch() method or simply by checking the formState.invalid. ->React Hook From Docu

steyer-mika avatar Feb 12 '24 19:02 steyer-mika

@dwk601

If you want to use input element with type='number' then the zod validator is used as shown in the issue https://github.com/shadcn-ui/ui/issues/421#issuecomment-1890398977

imopbuilder avatar Feb 14 '24 09:02 imopbuilder

This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you.

shadcn avatar Jun 04 '24 23:06 shadcn