33
loading...
This website collects cookies to deliver better user experience
const UserForm = () => {
return (
<Formik
initialValues={{
firstname: ''
lastname: ''
email: ''
country: ''
state: ''
zip: ''
}}
onSubmit={() => {
console.log('form submitted')
}}
>
{ ({
values,
errors,
touched,
handleChange,
handleBlur,
handleSubmit
}) => (
<div className="container">
<div className="col-md-12 mt-5">
<form onSubmit={handleSubmit}>
<h4 className="mb-3">Personal information</h4>
<div className="row">
<div className="col-md-6 mb-3">
<label htmlFor="firstname">First name</label>
<input
type="text"
className="form-control"
id="firstname"
name="firstname"
value={values.firstname}
/>
</div>
<div className="col-md-6 mb-3">
<label htmlFor="lastname">Last name</label>
<input
type="text"
className="form-control"
id="lastname"
name="lastname"
value={values.lastname}
/>
</div>
</div>
<div className="mb-3">
<label htmlFor="email">Email</label>
<input
type="email"
className="form-control"
id="email"
name="email"
placeholder="[email protected]"
value={values.email}
/>
</div>
<div className="row">
<div className="col-md-5 mb-3">
<label htmlFor="country">Country</label>
<select
className="custom-select d-block w-100"
id="country"
name="country"
value={values.country}
>
<option value="">Choose...</option>
<option value="NIG">Nigeria</option>
<option value="GH">Ghana</option>
<option value="SA">South Africa</option>
</select>
</div>
<div className="col-md-4 mb-3">
<label htmlFor="state">State</label>
<select
className="custom-select d-block w-100"
id="state"
name="state"
value={values.state}
>
<option value="">Choose...</option>
<option value="lagos">Lagos</option>
<option value="east legion">East Legion</option>
<option value="cape town">Cape Town</option>
</select>
</div>
<div className="col-md-3 mb-3">
<label htmlFor="zip">Zip</label>
<input
type="text"
className="form-control"
id="zip"
name="zip"
value={values.zip}
/>
</div>
</div>
<hr className="mb-4"/>
<button className="btn btn-primary btn-lg btn-block" type="submit">
Submit
</button>
</form>
</div>
</div>
) }
</Formik>
)
}
npm install yup
in our command line. Make sure the current directory you are in when running this command is your React project folder. userForm.js
. It is in this file we will be importing Yup as follows:import * as Yup from 'yup'
validationSchema
that we can add to the Formik
componentvalidationSchema = {Yup.object({
firstname: Yup
.string()
.required('Sorry, this is required')
.max(5, 'Sorry, name is too long'),
lastname: Yup
.string()
.required('Sorry, this is required'),
email: Yup
.string()
.required('Sorry, this is required')
.email('Invalid email format')
})}
validationSchema
, which in turn gives us access to several methods that we can use for validation. The strings provided in some of the methods are the error messages that will be displayed if a particular input field fails a validation.form
component when an input field fails a validation.<div className="container">
<div className="col-md-12 mt-5">
<form onSubmit={handleSubmit}>
<h4 className="mb-3">Personal information</h4>
<div className="row">
<div className="col-md-6 mb-3">
<label htmlFor="firstname">First name</label>
<input
type="text"
className="form-control"
id="firstname"
name="firstname"
value={values.firstname}
onChange={handleChange}
onBlur={handleBlur}
/>
{errors.firstname && touched.firstname ?
<span style={{color: 'red'}}>
{errors.firstname}
</span>
: null}
</div>
<div className="col-md-6 mb-3">
<label htmlFor="lastname">Last name</label>
<input
type="text"
className="form-control"
id="lastname"
name="lastname"
value={values.lastname}
onChange={handleChange}
onBlur={handleBlur}
/>
{errors.lastname && touched.lastname ?
<span style={{color: 'red'}}>
{errors.lastname}
</span>
: null}
</div>
</div>
<div className="mb-3">
<label htmlFor="email">Email</label>
<input
type="email"
className="form-control"
id="email"
name="email"
placeholder="[email protected]"
value={values.email}
onChange={handleChange}
onBlur={handleBlur}
/>
{errors.email && touched.email ?
<span style={{color: 'red'}}>
{errors.firstname}
</span>
: null}
</div>
<div className="row">
<div className="col-md-5 mb-3">
<label htmlFor="country">Country</label>
<select
className="custom-select d-block w-100"
id="country"
name="country"
value={values.country}
onChange={handleChange}
>
<option value="">Choose...</option>
<option value="NIG">Nigeria</option>
<option value="GH">Ghana</option>
<option value="SA">South Africa</option>
</select>
</div>
<div className="col-md-4 mb-3">
<label htmlFor="state">State</label>
<select
className="custom-select d-block w-100"
id="state"
name="state"
value={values.state}
onChange={handleChange}
>
<option value="">Choose...</option>
<option value="lagos">Lagos</option>
<option value="east legion">East Legion</option>
<option value="cape town">Cape Town</option>
</select>
</div>
<div className="col-md-3 mb-3">
<label htmlFor="zip">Zip</label>
<input
type="text"
className="form-control"
id="zip"
name="zip"
value={values.zip}
onChange={handleChange}
/>
</div>
</div>
<hr className="mb-4"/>
<button className="btn btn-primary btn-lg btn-block" type="submit">
Submit
</button>
</form>
</div>
</div>
validationSchema
from the errors object. To display the error message, errors.firstname
checks if there was an error when validating the firstname input field and touched.firstname
checks if the firstname input field was accessed or clicked on by the user. If both conditions passes, we display an error below the input field, else, no error is displayed.validationSchema
as a property to the Formik
looks like this:const UserForm = () => {
return (
<Formik
initialValues={{
firstname: ''
lastname: ''
email: ''
country: ''
state: ''
zip: ''
}}
validationSchema = {Yup.object({
firstname: Yup
.string()
.required('Sorry, this is required')
.max(5, 'Sorry, name is too long'),
lastname: Yup
.string()
.required('Sorry, this is required'),
email: Yup
.string()
.required('Sorry, this is required')
.email('Invalid email format')
})}
onSubmit={() => {
console.log('form submitted')
}}
>
{ ({
values,
errors,
touched,
handleChange,
handleBlur,
handleSubmit
}) => (
<div className="container">
<div className="col-md-12 mt-5">
<form onSubmit={handleSubmit}>
<h4 className="mb-3">Personal information</h4>
<div className="row">
<div className="col-md-6 mb-3">
<label htmlFor="firstname">First name</label>
<input
type="text"
className="form-control"
id="firstname"
name="firstname"
value={values.firstname}
onChange={handleChange}
onBlur={handleBlur}
/>
{errors.firstname && touched.firstname ?
<span style={{color: 'red'}}>
{errors.firstname}
</span>
: null}
</div>
<div className="col-md-6 mb-3">
<label htmlFor="lastname">Last name</label>
<input
type="text"
className="form-control"
id="lastname"
name="lastname"
value={values.lastname}
onChange={handleChange}
onBlur={handleBlur}
/>
{errors.lastname && touched.lastname ?
<span style={{color: 'red'}}>
{errors.lastname}
</span>
: null}
</div>
</div>
<div className="mb-3">
<label htmlFor="email">Email</label>
<input
type="email"
className="form-control"
id="email"
name="email"
placeholder="[email protected]"
value={values.email}
onChange={handleChange}
onBlur={handleBlur}
/>
{errors.email && touched.email ?
<span style={{color: 'red'}}>
{errors.email}
</span>
: null}
</div>
<div className="row">
<div className="col-md-5 mb-3">
<label htmlFor="country">Country</label>
<select
className="custom-select d-block w-100"
id="country"
name="country"
value={values.country}
onChange={handleChange}
>
<option value="">Choose...</option>
<option value="NIG">Nigeria</option>
<option value="GH">Ghana</option>
<option value="SA">South Africa</option>
</select>
</div>
<div className="col-md-4 mb-3">
<label htmlFor="state">State</label>
<select
className="custom-select d-block w-100"
id="state"
name="state"
value={values.state}
onChange={handleChange}
>
<option value="">Choose...</option>
<option value="lagos">Lagos</option>
<option value="east legion">East Legion</option>
<option value="cape town">Cape Town</option>
</select>
</div>
<div className="col-md-3 mb-3">
<label htmlFor="zip">Zip</label>
<input
type="text"
className="form-control"
id="zip"
name="zip"
value={values.zip}
onChange={handleChange}
/>
</div>
</div>
<hr className="mb-4"/>
<button className="btn btn-primary btn-lg btn-block" type="submit">
Submit
</button>
</form>
</div>
</div>
) }
</Formik>
)
}
handleChange
to handle update and the values
object holds all the current values. Same with the errors
object we use to display the error messages for individual fields.