每当 React Formik 中发生错误时更改现有输入字段的样式

Change style of existing input fields whenever error occurs in React Formik

在我使用 React Formik 库创建的 React Form 中,我想将输入元素的边框颜色样式更改为 1px 纯红色每当出现错误时。

但正如我们所知,只要发现新的验证错误,react formik 就会使用 ErrorMessage 呈现一个新的错误组件,所以基本上我们如何更改 现有的输入字段 错误时的颜色?

CSS :

.formGroup input {
  width: 100%;
  border: none;
  border-bottom: 1px solid black;
}
.formGroup input:focus {
  outline: none;
}
.formGroup button {
  background-color: antiquewhite;
  border-radius: 3px;
  border: none;
  font-size: 15px;
  padding: 10px;
}
.error {
  color: red;
  text-align: start;
  font-size: 13px;
}

代码:

import React, { Component } from "react";
import classes from "./P.module.css";
import { NavLink } from "react-router-dom";
import { Formik, Form, Field, ErrorMessage } from "formik";
import * as Yup from "yup";
const initialValues = {
  company_name: "",
  company_profile: "The Opinia is news and media company",
  location: "",
  phone_number: "",
  email: "",
  gst: "",
};

const onSubmit = (values) => {
  console.log("Form Data: ", values);
};

// for validation
const validationSchema = Yup.object({
  company_name: Yup.string().required("Required"),
  company_profile: Yup.string().required("Required"),
  location: Yup.string().required("Required"),
  email: Yup.string().required("Required").email("Invalid email format"),
  phone_number: Yup.number().required("Required"),
  gst: Yup.string().required("Required"),
});
export default function Projects2(props) {
  return (
    <div className={classes.main}>
      <Formik
        initialValues={initialValues}
        validationSchema={validationSchema}
        onSubmit={onSubmit}
      >
        <Form className={classes.form}>
          <div className={`${classes.formGroup}`}>
            <label htmlFor="company_name">Company Name</label>
            <Field type="text" id="company_name" name="company_name" />
            <ErrorMessage name="company_name" component={TextError} />
          </div>
          <div className={classes.formGroup}>
            <label htmlFor="company_profile">Company Profile</label>
            <Field type="text" id="company_profile" name="company_profile" />
            <ErrorMessage name="company_profile" component={TextError} />
          </div>
          <div className={classes.formGroup}>
            <label htmlFor="location">Location</label>
            <Field type="text" id="location" name="location" />
            <ErrorMessage name="location" component={TextError} />
          </div>
          <div className={classes.formGroup}>
            <label htmlFor="phone_number">Phone</label>
            <Field type="number" id="phone_number" name="phone_number" />
            <ErrorMessage name="phone_number" component={TextError} />
          </div>
          <div className={classes.formGroup}>
            <label htmlFor="email">Email</label>
            <Field type="email" id="email" name="email" />
            <ErrorMessage name="email" component={TextError} />
          </div>
          <div className={classes.formGroup}>
            <label htmlFor="gst">GST Number</label>
            <Field type="text" id="gst" name="gst" />
            <ErrorMessage name="gst" component={TextError} />
          </div>

          <div className={classes.formGroup}>
            <button type="submit">Submit</button>
          </div>
        </Form>
      </Formik>
    </div>
  );
}

export function TextError(props) {
  return <div className={classes.error}>{props.children}</div>;
}

您可以使用 react formik 快速字段,以便通过 react js 表单执行验证

到目前为止,似乎没有一种简单的方法可以做到这一点。您可以为 <Field> 渲染自定义子项来实现此目的。示例:

<Field type="text" name="firstName">
  { ({ 
       field, 
       meta: { touched, error } 
    }) => <input className={ touched && error ? "invalid" : "" } { ...field } />
  }
</Field>

然后在css

input.invalid {
  border-color: red;
}

更新:这里是文档链接:field 输入的形状为 FieldInputProps and meta is a FieldMetaProps