基本上,我有一个带有客户端验证的字段,如下所示。但是服务器也可以出于任何原因拒绝该字段,但可以提供一个原因:例如“名字不能是管理员”。对于这种情况,我想显示发送回服务器的错误消息。
虽然有效,但我在擦除服务器发送的错误消息时遇到了问题,该消息保存在状态中,然后显示在字段错误消息中。我想从onChangeText状态中删除服务器错误消息(当用户开始修复错误时),但是不能。现在的问题是在“退格”字段中输入了很多内容,因此没有数据输入到该字段中:
handleDataChange(fieldName, formIkProps){
/*
When server sends back incorrect/error fields, we set
the field as incorrect. The error message remains
Here, we just clean it up
@fieldName must be field from state.errorsList or formIk
@formIkProps: a formIk prop that gets called by the element
*/
this.state.errorsList[fieldName] = '';
formIkProps(fieldName)
}
<Formik
initialValues={initialValues}
onSubmit={values => this.handleSubmit(values)}
validationSchema={yup.object().shape({
firstName: yup
.string()
.min(3)
.max(25)
.required(),
})}>
{({
handleChange,
handleBlur,
handleSubmit,
values,
touched,
dirty,
isSubmitting,
errors,
setFieldValue
}) => (
<View>
<FormInput
name="firstName"
onChangeText={this.handleDataChange('firstName', handleChange)}
onBlur={handleBlur('firstName')}
autoFocus
errMsg={
(this.state.errorsList.firstName != '' ? this.state.errorsList.firstName : '' ) || (errors.firstName && touched.firstName ? errors.firstName : '')
}
/>
</View>
)}
</Formik>
表单提交到服务器后,将填充errorList。