I've been using Yup and Formik for form validation in React. Now when a user blurs an input then, errors are displayed. Here is my code: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
import React from "react";
import { Formik, Field } from "formik";
import TextField from "@material-ui/core/TextField";
import Typography from "@material-ui/core/Typography";
import * as yup from "yup";
const FirstNameInputComponent = ({ field, ...props }) => {
const { errorMessage, touched } = props;
const { name, value, onChange, onBlur } = field;
return (
<TextField
value={value}
name={name}
error={touched && errorMessage ? true : false}
label="نام"
helperText={touched && errorMessage ? errorMessage : undefined}
onChange={onChange}
onBlur={onBlur}
/>
);
};
const App = () => {
const validationSchema = yup.object().shape({
first_name: yup.string().required()
});
return (
<Formik
initialValues={{
file: undefined,
text: undefined
}}
validationSchema={validationSchema}
validateOnBlur={true}
render={({
values,
errors,
touched,
handleChange,
handleBlur,
setFieldValue
}) => {
return (
<form>
<Field
name="first_name"
component={FirstNameInputComponent}
errorMessage={errors["first_name"]}
touched={touched["first_name"]}
onChange={handleChange}
onBlur={handleBlur}
/>
</form>
/>
);
}
How can I display errors simultaneously with a user writing in an input?