[英]Formik button not disabled initially
I am using Formik validation and I am trying to disable the button if the validation schema is not fulfilled.我正在使用 Formik 验证,如果未满足验证模式,我将尝试禁用该按钮。 It's supposed to be a phone number so if I type alphabets, the button is disabled.它应该是一个电话号码,所以如果我输入字母,该按钮将被禁用。 It works.有用。
However, initially when I open the screen and the text field is empty, the button should still be disabled.但是,最初当我打开屏幕并且文本字段为空时,该按钮仍应被禁用。 But it's not.但事实并非如此。 Even though the validation schema says 'required'.即使验证模式说“必需”。 How can I fix this?我怎样才能解决这个问题?
<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.'),
});
Try setting the isInitialValid
prop on the Formik
component to false
.尝试将Formik
组件上的isInitialValid
属性设置为false
。
<Formik
initialValues={initialValues}
onSubmit={handleSubmitForm}
validationSchema={validationSchema}
isInitialValid={false}
>
...
</Formik>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.