React Formik - 仅在表单提交时触发验证
React Formik - Trigger validation only on form submit
我在我的 React Native 应用程序中使用 Formik。在登录表单上,我有两个字段:电子邮件和密码,它们都是必需的。
我写过这样的验证规则:
export const LoginSchema = Yup.object().shape({
email: Yup.string()
.email('The email is invalid')
.required('This field is required'),
password: Yup.string()
.min(6, 'The password is too short')
.max(12, 'The password is too long')
.required('This field is required'),
});
我只需要在提交表单时触发验证并显示错误弹出窗口。我已阅读文档,但找不到解决方案,因为验证会触发 onBlur。如何做到这一点?
谢谢!
const Login = ({ navigation }) => {
const [isLoading, setIsLoading] = useState(true);
const [isVisible, setIsVisible] = useState(false);
useEffect(() => {
// Later check for token
const tokenIsStored = true;
if (tokenIsStored) {
setIsLoading(false);
}
});
const onLogin = values => {
console.log(values, 'on login');
// Pass value to BE endpoint
navigation.navigate('Dashboard');
};
const onModalClose = () => {
setIsVisible(false);
};
console.log(isVisible);
if (!isLoading) {
return (
<ScrollContainer keyboardShouldPersistTaps="handled">
<ThemedStatusBar />
<ThemedModal
isVisible={isVisible}
primaryMessage="Log In Failed"
secondaryMessage="Please check your password"
btnTitle="OK"
btnPress={() => onModalClose()}
/>
<Formik
initialValues={{ email: '', password: '' }}
validationSchema={LoginSchema}
onSubmit={values => onLogin(values)}
>
{props => (
<View>
<ScrollContainer BackgroundColor={Colors.greyColor} Padding="0px" style={styles.loginForm}>
<ThemedInput
onChangeText={props.handleChange('email')}
onBlur={props.handleBlur('email')}
value={props.values.email}
placeholder="Email"
keyboardType="email-address"
/>
<ThemedInput
onChangeText={props.handleChange('password')}
onBlur={props.handleBlur('password')}
value={props.values.password}
placeholder="Password"
overrideStyles={styles.loginInputBottom}
secureTextEntry
/>
{props.errors.email && setIsVisible(true)}
</ScrollContainer>
<ThemedButton onPress={props.handleSubmit} title="Log In" />
</View>
)}
</Formik>
</ScrollContainer>
);
}
return <ThemedLoader isLoading />;
};
export default Login;
勾选docs
You can control when Formik runs validation by changing the values of <Formik validateOnChange>
and/or <Formik validateOnBlur>
props depending on your needs. By default, Formik will run validation methods as follows:
将道具 validateOnChange={false}
和 validateOnBlur={false}
传递给您的 Formik
是的。你可以这样做。
<Formik
initialValues={initialValues}
validationSchema={validationSchema}
validateOnChange={false}
validateOnBlur={false}
onSubmit={(values, { validate }) => {
validate(values);
}}
>
如果使用useFormik hook,需要添加下一个配置
const formik = useFormik({
initialValues,
validationSchema,
validateOnChange: false, // this one
validateOnBlur: false, // and this one
onSubmit: (values) => {
do something on submit
},
});
我在我的 React Native 应用程序中使用 Formik。在登录表单上,我有两个字段:电子邮件和密码,它们都是必需的。
我写过这样的验证规则:
export const LoginSchema = Yup.object().shape({
email: Yup.string()
.email('The email is invalid')
.required('This field is required'),
password: Yup.string()
.min(6, 'The password is too short')
.max(12, 'The password is too long')
.required('This field is required'),
});
我只需要在提交表单时触发验证并显示错误弹出窗口。我已阅读文档,但找不到解决方案,因为验证会触发 onBlur。如何做到这一点?
谢谢!
const Login = ({ navigation }) => {
const [isLoading, setIsLoading] = useState(true);
const [isVisible, setIsVisible] = useState(false);
useEffect(() => {
// Later check for token
const tokenIsStored = true;
if (tokenIsStored) {
setIsLoading(false);
}
});
const onLogin = values => {
console.log(values, 'on login');
// Pass value to BE endpoint
navigation.navigate('Dashboard');
};
const onModalClose = () => {
setIsVisible(false);
};
console.log(isVisible);
if (!isLoading) {
return (
<ScrollContainer keyboardShouldPersistTaps="handled">
<ThemedStatusBar />
<ThemedModal
isVisible={isVisible}
primaryMessage="Log In Failed"
secondaryMessage="Please check your password"
btnTitle="OK"
btnPress={() => onModalClose()}
/>
<Formik
initialValues={{ email: '', password: '' }}
validationSchema={LoginSchema}
onSubmit={values => onLogin(values)}
>
{props => (
<View>
<ScrollContainer BackgroundColor={Colors.greyColor} Padding="0px" style={styles.loginForm}>
<ThemedInput
onChangeText={props.handleChange('email')}
onBlur={props.handleBlur('email')}
value={props.values.email}
placeholder="Email"
keyboardType="email-address"
/>
<ThemedInput
onChangeText={props.handleChange('password')}
onBlur={props.handleBlur('password')}
value={props.values.password}
placeholder="Password"
overrideStyles={styles.loginInputBottom}
secureTextEntry
/>
{props.errors.email && setIsVisible(true)}
</ScrollContainer>
<ThemedButton onPress={props.handleSubmit} title="Log In" />
</View>
)}
</Formik>
</ScrollContainer>
);
}
return <ThemedLoader isLoading />;
};
export default Login;
勾选docs
You can control when Formik runs validation by changing the values of
<Formik validateOnChange>
and/or<Formik validateOnBlur>
props depending on your needs. By default, Formik will run validation methods as follows:
将道具 validateOnChange={false}
和 validateOnBlur={false}
Formik
是的。你可以这样做。
<Formik
initialValues={initialValues}
validationSchema={validationSchema}
validateOnChange={false}
validateOnBlur={false}
onSubmit={(values, { validate }) => {
validate(values);
}}
>
如果使用useFormik hook,需要添加下一个配置
const formik = useFormik({
initialValues,
validationSchema,
validateOnChange: false, // this one
validateOnBlur: false, // and this one
onSubmit: (values) => {
do something on submit
},
});