import React from 'react';
import { Formik, Field, Form, ErrorMessage } from 'formik';
import * as Yup from 'yup';
const SignupForm = () => {
return (
<Formik
initialValues={{ firstName: '', lastName: '', email: '' }}
validationSchema={Yup.object({
firstName: Yup.string()
.max(15, 'Must be 15 characters or less')
.required('Required'),
lastName: Yup.string()
.max(20, 'Must be 20 characters or less')
.required('Required'),
email: Yup.string().email('Invalid email address').required('Required'),
})}
onSubmit={(values, { setSubmitting }) => {
setTimeout(() => {
alert(JSON.stringify(values, null, 2));
setSubmitting(false);
}, 400);
}}
>
<Form>
<label htmlFor="firstName">First Name</label>
<Field name="firstName" type="text" />
<ErrorMessage name="firstName" />
<label htmlFor="lastName">Last Name</label>
<Field name="lastName" type="text" />
<ErrorMessage name="lastName" />
<label htmlFor="email">Email Address</label>
<Field name="email" type="email" />
<ErrorMessage name="email" />
<button type="submit">Submit</button>
</Form>
</Formik>
);
};
import React from "react";
import { Formik, Form, Field, ErrorMessage } from "formik";
import * as Yup from "yup";
import "bootstrap/dist/css/bootstrap.css";
const LoginSchema = Yup.object().shape({
email: Yup.string()
.email("Invalid email address format")
.required("Email is required"),
password: Yup.string()
.min(3, "Password must be 3 characters at minimum")
.required("Password is required"),
});
class App extends React.Component {
render() {
return (
<div className="container">
<div className="row">
<div className="col-lg-12">
<Formik
initialValues={{ email: "", password: "" }}
validationSchema={LoginSchema}
onSubmit={(values) => {
console.log(values);
alert("Form is validated! Submitting the form...");
}}
>
{({ touched, errors, isSubmitting, values }) =>
!isSubmitting ? (
<div>
<div className="row mb-5">
<div className="col-lg-12 text-center">
<h1 className="mt-5">Login Form</h1>
</div>
</div>
<Form>
<div className="form-group">
<label htmlFor="email">Email</label>
<Field
type="email"
name="email"
placeholder="Enter email"
autocomplete="off"
className={`mt-2 form-control
${touched.email && errors.email ? "is-invalid" : ""}`}
/>
<ErrorMessage
component="div"
name="email"
className="invalid-feedback"
/>
</div>
<div className="form-group">
<label htmlFor="password" className="mt-3">
Password
</label>
<Field
type="password"
name="password"
placeholder="Enter password"
className={`mt-2 form-control
${
touched.password && errors.password
? "is-invalid"
: ""
}`}
/>
<ErrorMessage
component="div"
name="password"
className="invalid-feedback"
/>
</div>
<button
type="submit"
className="btn btn-primary btn-block mt-4"
>
Submit
</button>
</Form>
</div>
) : (
<div>
<h1 className="p-3 mt-5">Form Submitted</h1>
<div className="alert alert-success mt-3">
Thank for your connecting with us. Here's what we got from
you !
</div>
<ul className="list-group">
<li className="list-group-item">Email: {values.email}</li>
<li className="list-group-item">
Password: {values.password}
</li>
</ul>
</div>
)
}
</Formik>
</div>
</div>
</div>
);
}
}
export default App;