首页 > 解决方案 > react-hook-form + materialUI + 是的:需要如何制作文件

问题描述

我有

  const validationSchema = Yup.object().shape({
    photo: Yup.mixed().required("File is required"),
  });
  const {
    control,
    handleSubmit,
    formState: { errors },
    reset,
    setError,
  } = useForm({
    resolver: yupResolver(validationSchema),
  });

以及使用材质 UI 进行文件输入的代码

    {/********************************************************
    PHOTO
    *********************************************************/}
    <Controller
      type="file"
      name="photo"
      control={control}
      render={({ field }) => (
        <TextField
          inputRef={field.ref}
          fullWidth
          label="photo"
          margin="dense"
          accept="image/*"
          type="file"
          onChange={(e) => {
            field.onChange(e.target.files);
            handleChange(e);
          }}
          InputLabelProps={{
            shrink: true,
          }}
          error={errors.photo ? true : false}
        />
      )}
    />
    <Typography variant="inherit" color="textSecondary">
      {errors.photo?.message}
    </Typography>

标签: material-uireact-hook-formyup

解决方案


推荐阅读