javascript - Formik 按钮最初未禁用
问题描述
我正在使用 Formik 验证,如果未满足验证模式,我将尝试禁用该按钮。它应该是一个电话号码,所以如果我输入字母,该按钮将被禁用。有用。
但是,最初当我打开屏幕并且文本字段为空时,该按钮仍应被禁用。但事实并非如此。即使验证模式说“必需”。我怎样才能解决这个问题?
<Formik
initialValues={initialValues}
onSubmit={handleSubmitForm}
validationSchema={validationSchema}>
{({ handleChange, handleBlur, handleSubmit, values, isValid }) => (
<View style={styles.searchFieldContainer}>
<View style={styles.form}>
<FieldInput style={styles.fieldInput}
handleChange={handleChange}
handleBlur={handleBlur}
value={values.phoneNumber}
fieldType="phoneNumber"
icon="phone"
placeholderText="49152901820"
/>
<ErrorMessage
name="phoneNumber"
render={(msg) => (
<Text style={styles.errorText}>{msg}</Text>
)}
/>
</View>
<View style={styles.buttonContainer}>
<Button
block
success
disabled={!isValid}
onPress={handleSubmit}
style={styles.button}>
<Text>Speichern</Text>
</Button>
</View>
</View>
)}
</Formik>
const phoneNumberValidationSchema = yup.object().shape({
phoneNumber: yup
.string()
.label('phoneNumber')
.required('Bitte gebe deine Handynummer ein.')
.matches(/^[0-9]*$/, 'Bitte nur Ziffern eingeben.'),
});
解决方案
尝试将组件isInitialValid
上的 prop设置为.Formik
false
<Formik
initialValues={initialValues}
onSubmit={handleSubmitForm}
validationSchema={validationSchema}
isInitialValid={false}
>
...
</Formik>
推荐阅读
- kubernetes - 在 Kubernetes 中注册多个自定义指标 API
- c# - 拖放中自定义接受的操作
- neural-network - Caffe CNN Slice 层:2nd Slice 层产生未知的底部 blob
- java - 在 Android 的 FirestoreRecyclerAdapter 中显示 ServerValue.TIMESTAMP
- continuous-integration - 在哪里存储 CI 管道期间生成的 VSTS 扩展自定义数据
- c++ - 如何等待窗口在 Qt 中被隐藏?
- python - 为什么virtualenv在被拉取后依赖全局python而不是本地python?
- flutter - 在 initState 或类构造函数中定义小部件而不是构建?
- python - 在 pycharm (Windows) 中找不到模块
- python - 用户定义的对象作为 Python 中函数指定的参数类型