I am making a web project using React, Material, Formik, formik-material-ui.
I have made a Formik form using yup for validation.
const schema = yup.object({
name: yup.string().trim().lowercase().required("Name is required."),
});
<Formik
initialValues={{
name: "",
}}
validationSchema={schema}
>
{({ submitForm, isSubmitting, handleSubmit, handleChange, values }) => (
<Form noValidate onSubmit={handleSubmit}>
<Grid container direction="row" spacing={2}>
<Grid container item xs={12} spacing={4}>
<Grid item xs={4}>
<InputLabel>Patient Name *</InputLabel>
<TextField fullWidth name="name" type="text" />
<InputLabel>Patient ID: P0006</InputLabel>
</Grid>
</Grid>
</Grid>
</Form>
)}
</Formik>
The TextField is a custom component as follows
import React, { Fragment } from "react";
import { Field, ErrorMessage } from "formik";
import { TextField } from "libs/formik-material-ui/src";
const TextFieldStyle = {
padding: 8,
fontSize: "0.75rem",
};
export default React.memo((props: any) => {
return (
<Fragment>
<Field
component={TextField}
inputProps={{
style: TextFieldStyle,
}}
size="small"
margin="none"
variant="outlined"
{...props} // add props at the key to override any user defined similar props
>
{props.children}
</Field>
<ErrorMessage name={props.name}>{(msg) => <div style={{ color: "red", textAlign: "left" }}>{msg}</div>}</ErrorMessage>
</Fragment>
);
});
Since I want to display ErrorMessage of a different style and not the default one, I have added below the field. But with this approach, the error message is being printed twice.
How can I disable the default message from being printed ?
