简体   繁体   English

提交表单后删除 Formik 错误消息

[英]delete Formik Error Message after submitting form

Upon entering my screen, there's no error displayed on the input field.进入我的屏幕后,输入字段上没有显示错误。

In my form, I take an input and run a graphql mutation on it.在我的表单中,我接受一个输入并在其上运行 graphql 突变。 Once it's done, I reset the form.完成后,我重置表单。 However, after resetting, I start seeing a Formik Error because the field is .required() and currently it's empty.但是,重置后,我开始看到 Formik 错误,因为该字段是.required()并且当前为空。

This error should only be shown when I am trying to submit the form without an input.只有当我尝试在没有输入的情况下提交表单时,才会显示此错误。 It shouldn't show after I have submitted it once successfully.我提交成功后它不应该显示。

  const handleSubmitForm = (
    values: FormValues,
    helpers: FormikHelpers<FormValues>,
  ) => {
    editLocationName({
      variables: {
      favouritePlaceId: route.params.id,
      input: {customisedName: values.locationName}
      },
    });
    helpers.resetForm();
    helpers.setErrors({});
  };
.....


          <Formik
            initialValues={initialValues}
            onSubmit={handleSubmitForm}
            validationSchema={validationSchema}>
            {({ handleChange, handleBlur, handleSubmit, values }) => (
              <View style={styles.searchFieldContainer}>
                <View style={styles.form}>
                  <FieldInput
                    handleChange={handleChange}
                    handleBlur={handleBlur}
                    value={values.locationName}
                    fieldType="locationName"
                    placeholderText="Neuer Name"
                  />
                  <ErrorMessage
                    name="locationName"
                    render={(msg) => <ErrorText errorMessage={msg} />}
                  />
                </View>
                <View style={styles.buttonContainer}>
                  <ActionButton buttonText="Save" onPress={handleSubmit} />
                </View>
              </View>
            )}
          </Formik>

Validation Schema:验证架构:

const favouriteLocationNameValidationSchema = yup.object().shape({
  locationName: yup
  .string()
  .label('locationName')
  .required('Required Field')
});

How can I reset the error message along with the form?如何重置错误消息和表单?

setErrors({}) did not work for me. setErrors({}) 对我不起作用。

Adding it here, because suggestion requires code .在这里添加它,因为建议需要代码。 Add following code and let me know if it helped.添加以下代码,如果有帮助,请告诉我。

helpers.resetForm({
 errors: {},
 touched: {}
});

Remove helpers.setErrors({});

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

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