简体   繁体   English

为什么表单字段不会设置为脏 onSubmit? 反应挂钩形式

[英]why form fields won't set as dirty onSubmit ? React-Hook-Form

Ok guys what we usaully do is if user touched the input then we validate it and show the message.好吧,伙计们,我们通常做的是,如果用户触摸了输入,那么我们会验证它并显示消息。 this is ok but when user click the submit then all the fields should set to dirty and all error message should be visible.这没关系,但是当用户单击提交时,所有字段都应设置为脏,并且所有错误消息都应该可见。 but this won't happen in my code但这不会发生在我的代码中

  const methods = useForm<FormProps>({
    mode: 'onTouched',
    defaultValues: {
      ...user.data,
    },
    resolver: yupResolver(validationSchema),
  });

        <ColumnStack spacing={0}>
          <InputLabel>First Name</InputLabel>
          <Controller
            control={control}
            name='firstName'
            render={({ field, formState }) => (
              <TextField
                placeholder='Enter Your First Name'
                error={formState.dirtyFields.firstName && formState.errors.firstName.message}
                helperText={formState.dirtyFields.firstName && formState.errors.firstName.message}
                disabled={loading}
                {...field}
              />
            )}
          />
        </ColumnStack>


how should i fix this?我应该怎么解决这个问题? when user click on submit all form fields won't set as dirty.当用户单击提交时,所有表单字段都不会设置为脏。 I tried changing validation mode to 'onChange', 'onSubmit', 'all' but none of them solved my problem我尝试将验证模式更改为“onChange”、“onSubmit”、“all”,但它们都没有解决我的问题

You can use您可以使用

setValue("fieldName", value, { shouldDirty: true }) when updating a form value setValue("fieldName", value, { shouldDirty: true })更新表单值时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM