微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

验证错误消息在本机反应中没有改变

如何解决验证错误消息在本机反应中没有改变

我正在学习如何使用 yup 验证,这是我的代码。 但验证错误消息不会改变。

import React from "react";
import { StyleSheet,Image } from "react-native";
import * as Yup from "yup";

import { Formik } from "formik";

import AppTextInput from "../components/AppTextInput";
import Screen from "../components/Screen";
import AppButton from "../components/AppButton";
import ErrorMessage from "../components/ErrorMessage";

const validationSchema = Yup.object().shape({
  email: Yup.string().required().email("Please enter a valid email").label("Email"),password: Yup.string().required().min(4,"To short").label("Password"),});

function LoginScreen() {
  return (
    <Screen style={styles.container}>
      <Image style={styles.logo} source={require("../assets/logo-red.png")} />
      <Formik
        initialValues={{ email: "",password: "" }}
        onSubmit={(values) => console.log(values)}
        validationSchema={validationSchema}
      >
        {({ handleChange,handleSubmit,errors,setFieldTouched,touched }) => (
          <>
            <AppTextInput
              autoCapitalize="none"
              autoCorrect={false}
              icon="email"
              keyboardType="email-address"
              onBlur={() => setFieldTouched("email")}
              name="email"
              placeholder="Email"
              textContentType="emailAddress"
            />
            <ErrorMessage visible={touched.email} error={errors.email} />
            <AppTextInput
              autoCapitalize="none"
              autoCorrect={false}
              icon="lock"
              name="password"
              placeholder="Password"
              onBlur={() => setFieldTouched("password")}
              secureTextEntry
              textContentType="password"
            />
            <ErrorMessage visible={touched.password} error={errors.password} />
            <AppButton onPress={handleSubmit} title="Login" />
          </>
        )}
      </Formik>
    </Screen>
  );
}

const styles = StyleSheet.create({
  container: {
    padding: 10,},logo: {
    width: 80,height: 80,alignSelf: "center",marginTop: 50,marginBottom: 20,});

export default LoginScreen;

解决方法

您没有编写 onChangeText 道具

像这样写两个AppTextInput

<AppTextInput
   autoCapitalize="none"
   autoCorrect={false}
   onChangeText={handleChange('email')} // This was missing
   icon="email"
   keyboardType="email-address"
   onBlur={() => setFieldTouched('email')}
   name="email"
   placeholder="Email"
   textContentType="emailAddress"
/>

<AppTextInput
   autoCapitalize="none"
   autoCorrect={false}
   onChangeText={handleChange('password')} // This was missing
   icon="lock"
   name="password"
   placeholder="Password"
   onBlur={() => setFieldTouched('password')}
   secureTextEntry
   textContentType="password"
 />

Working Example

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?