【发布时间】:2020-08-30 15:40:00
【问题描述】:
进入我的屏幕后,输入字段上没有显示错误。
在我的表单中,我接受一个输入并在其上运行一个 graphql 突变。完成后,我重置表格。但是,重置后,我开始看到 Formik 错误,因为该字段是 .required() 并且目前它是空的。
仅当我尝试在没有输入的情况下提交表单时才会显示此错误。提交成功后就不应该显示了。
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>
验证架构:
const favouriteLocationNameValidationSchema = yup.object().shape({
locationName: yup
.string()
.label('locationName')
.required('Required Field')
});
如何重置错误信息以及表单?
setErrors({}) 对我不起作用。
【问题讨论】:
-
helpers.resetForm()就足够了。它重置了一切 -
不,它没有。也请看这个qs。即使没有keyboard.dismiss(),当我触摸屏幕或尝试滚动它时,错误就会开始出现stackoverflow.com/questions/63654972/…@ShubhamVerma
标签: javascript reactjs typescript react-native formik