28
loading...
This website collects cookies to deliver better user experience
cd documents
bootstrap
to add a little bit of styles to our form by using the classes provided by bootstrap
.npx create-react-app formik-forms
cd formik-forms
npm install react-bootstrap bootstrap
npm install formik
yarn start
userForm.js
. Add the following code to the new file created:import React from 'react'
import {Formik} from 'formik'
import 'bootstrap/dist/css/bootstrap.min.css'
userForm.js
file or better still create your own using bootstrap<div className="container">
<div className="col-md-12 mt-5">
<form>
<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"/>
</div>
<div className="col-md-6 mb-3">
<label htmlFor="lastname">Last name</label>
<input type="text" className="form-control" id="lastname" name="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]"/>
</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">
<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">
<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"/>
</div>
</div>
<hr className="mb-4"/>
<button className="btn btn-primary btn-lg btn-block" type="submit">
Submit
</button>
</form>
</div>
</div>
Formik
component we imported earlier, but before that, we will have to declare our Formik
component and provide a function that gives us props with a lot of method that we will use inside our form.const UserForm = () => {
return (
<Formik>
{ ({
values,
errors,
touched,
handleChange,
handleBlur,
handleSubmit
}) => (
) }
</Formik>
)
}
values
, instead of props.values
. form
components in the Formik
function.const UserForm = () => {
return (
<Formik>
{ ({
values,
errors,
touched,
handleChange,
handleBlur,
handleSubmit
}) => (
<div className="container">
<div className="col-md-12 mt-5">
<form>
<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"/>
</div>
<div className="col-md-6 mb-3">
<label htmlFor="lastname">Last name</label>
<input type="text" className="form-control" id="lastname" name="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]"/>
</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">
<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">
<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"/>
</div>
</div>
<hr className="mb-4"/>
<button className="btn btn-primary btn-lg btn-block" type="submit">
Submit
</button>
</form>
</div>
</div>
) }
</Formik>
)
}
Formik
component requires properties. One a property that helps formik to know what we are going to be using on the form i.e the values that Formik
component will be controlling and another property that controls the submission of the form when a submit button is clicked.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>
<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"/>
</div>
<div className="col-md-6 mb-3">
<label htmlFor="lastname">Last name</label>
<input type="text" className="form-control" id="lastname" name="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]"/>
</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">
<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">
<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"/>
</div>
</div>
<hr className="mb-4"/>
<button className="btn btn-primary btn-lg btn-block" type="submit">
Submit
</button>
</form>
</div>
</div>
) }
</Formik>
)
}
form
component, you will notice all input components has an id
. It is these id
values that we use as the keys in the initialValues
object. For the form submission, I will just be displaying a string in the browser's console console.log('form submitted')
since we are not working with a server or database in this article.initialValues
property to the actual input we will be getting from our form by adding a value
property to the input
components and accessing users input from the values
method.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>
<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>
)
}
onSubmit
property to our form
component and pass in the handleSubmit
method from the Formik
component, and also add an onChange
property to the input components and pass in the handleChange
method.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>
)
}