create account

How to Easily Validate Forms with Yup and Formik by marvel1206

View this thread on: hive.blogpeakd.comecency.com
· @marvel1206 · (edited)
$25.74
How to Easily Validate Forms with Yup and Formik
![Blue Modern Tips Business Banner (1).png](https://files.peakd.com/file/peakd-hive/marvel1206/48aZBq9ovryQePNznxhyMtufJpU5nbykQUAuNeMknfWRQxLDPs2JRvHNAm73cuwDx2.jpg)


It is often said that if there is a framework or a library that makes your work easier, then use it. It is mainly the reason we code with React instead of Vanilla Javascript, or Tailwind Css/Bootstrap instead of just plain Css. These solutions exist to make things easy for us and that is the whole point of everything.

Validating forms, for example, can be done starting with using the Html "required" property to Vanilla Js, to Ajax. it really depends on how complex you want it to get. I have tried it in many ways and I found this bit easier, using Yup and Formik.


[**Yup**](https://www.npmjs.com/package/yup) is a Js schema builder for parsing values and validating subsequently, it can be very simple, or broad but then it makes the code cleaner and simpler. While [**Formik**](https://www.npmjs.com/package/formik) on the other hand, basically defines how we handle forms and errors. We will be using these two packages in this short tutorial.

We are going to run these three commands in our project directory.

```
Npx create-react-app .
Npm i yup formik --save

```



The first command creates a react app in our current directory, you can replace the period at the end and replace with your specified project directory. The second command installs yup and formik, they could be written in separate command but it's faster, writing it that way.

After that is done, we are going to create the structure for our page and setup three form inputs with a  Submit button. I am going to install a framework called Tailwind Css, it kind of helps me style faster. This is optional for you. You may decide to style with the standard css and you will still be good to go. I am going to go ahead and install this command for tailwind

```
npm i -D tailwindcss
npm tailwindcss init

```


Along with some other settings that can be found in their documentation [here](https://tailwindcss.com/docs/installation). 

PS: This is not a tutorial about tailwind css so I will provide little explanation in regards to that.



We have our project ready to go. I am going to define our webpage structure like so.
 ```
<div>
      <form>
      <input type="text"  placeholder='Username'/>
      <input type="email"  placeholder='email'/>
      <input type="text"  placeholder='password'/>
      <input type="text"  placeholder='confirm password'/>
      <button type="submit" name="submit" value="submit">Submit</button>
      </form>
    </div>


![IMG_20221004_225842.jpg](https://files.peakd.com/file/peakd-hive/marvel1206/Enz2yFsKn4AFpbWwtCx2JwzXcN5pbGRqf73gHUBjynSBbeLZLYPBDPSKmDKtMVHY2vw.jpg)

And style it up a bit
<div className='w-screen h-screen bg-blue-100'>
        
      <form className='w-full h-full'>
       
      <div className='flex flex-col gap-2 items-center justify-center h-full'>
      <div className='font-bold text-xl'>Validation</div> 
      <input className='w-[60%] border-2 p-2 border-gray-900' type="text"  placeholder='Username'/>
      <input type="email" className='w-[60%] border-2 p-2 border-gray-900' placeholder='Email'/>
      <input type="text" className='w-[60%] border-2 p-2 border-gray-900'  placeholder='Password'/>
      <input type="text" className='w-[60%] border-2 p-2 border-gray-900' placeholder='Confirm password'/>
      <button className='bg-sky-700 w-[40%] text-white rounded-md p-2 font-bold' type="submit" name="submit" value="submit">Submit</button>
      </div>
      </form>
     
     
    </div>

```

Ignore the ugly markup, just for the tutorial. We can now go ahead and import the Yup and Formik modules like so

```
import * as yup from "yup";
import { Formik, Form } from 'formik';

```


We can go ahead and implement yup straight away. I earlier mentioned that yup enables us to define our form inputs and the logic to it. We have the Name, Email, Password and Confirm Password. We are going to pass those to our yup schema, specify the kind of validation we want for each field.

Right now, we know all the input fields are required, we know the email field should contain a valid email field, we should also define a minimum password length and make sure it tallies with the 'confirm password' field.

```
let schema = yup.object().shape({
    username: yup.string().required("A username is required").min(3, "Too short"),
    email: yup.string().required("Email is required").email("Invalid email type"),
    password: yup.string().required("Password is required").min(6, "Too weak"),
    confirm: yup.string().required("").oneOf([yup.ref('password'), null], "Password does not match")
  
  
  })



```

We have defined the whole validation schema here.

The syntax is pretty much clear. At this point we can start utilizing our Formik. We are going to wrap our form tag with the <Formik></Formik> component and also replace the form tag with the <Form></Form> we imported like

```
import React from 'react'
import * as yup from "yup";
import { Formik, Form } from 'formik';


export default function App() {

  let schema = yup.object().shape({
    username: yup.string().required("A username is required").min(3, "Too short"),
    email: yup.string().required("Email is required").email("Invalid email type"),
    password: yup.string().required("Password is required").min(6, "Too weak"),
    confirm: yup.string().required("").oneOf([yup.ref('password'), null], "Password does not match")
  
  
  })
  
return (
    <div className='w-screen h-screen bg-blue-100'>
        <Formik>
        <Form className='w-full h-full'>
       
       <div className='flex flex-col gap-2 items-center justify-center h-full'>
       <div className='font-bold text-xl'>Validation</div> 
       <input className='w-[60%] border-2 p-2 border-gray-900' type="text"  placeholder='Name'/>
       <input type="email" className='w-[60%] border-2 p-2 border-gray-900' placeholder='Email'/>
       <input type="text" className='w-[60%] border-2 p-2 border-gray-900'  placeholder='Password'/>
       <input type="text" className='w-[60%] border-2 p-2 border-gray-900' placeholder='Confirm password'/>
       <button className='bg-sky-700 w-[40%] text-white rounded-md p-2 font-bold' type="submit" name="submit" value="submit">Submit</button>
       </div>
      
       </Form>
    
        </Formik>
     
     
    </div>
  )
}


```


The good thing about Formik is that it helps us handle things such as onChange, Onsubmit and others without having to create  separate functions for these. The important properties we are going to focus on are;

**validationschema**: We are going to reference the schema that contains our validation logic. In this case, the the yup schema we created earlier as 'schema'

**initialValues**: Initial values of our input properties.

onChange: Works as the onChange function you would traditionally create would work. Helps Formik keep track of the values
in your inputs. Formik has its own handleChange function.

**onBlur**: Helps Formik to know if an Input has been touched or not. This helps to display errors only if an input has been touched.
Formik also has its own handleBlur function.

**onSubmit**: Works with the handleSubmit function that submits the values to Formik.

**touched**: A boolean that can be either true or false depending if an input has been touched.

**errors**: An object of all the errors returned from our validation schema.

We are going to destructure these properties from formik so that we can use it with our inputs.  To do this, we need to pass the initialValues, validationSchema
to our Formik tag like;
 ```
<Formik
              initialValues={{
                username: "",
                email: "",
                password: ""
              }}
              validationSchema={schema}
              
              
              >

```


Then we destructure the aforementioned properties with a function that will return our Form like;

 ```
<Formik
              initialValues={{
                username: "",
                email: "",
                password: ""
              }}
              validationSchema={schema}
              
              
              >

                  {({handleChange, handleSubmit, handleBlur, touched, errors})=> (

                      <Form className='w-full h-full' >
                            
                      <div className='flex flex-col gap-2 items-center justify-center h-full'>
                      <div className='font-bold text-xl'>Validation</div> 
                      <input className='w-[60%] border-2 p-2 border-gray-900' type="text"  placeholder='Name'/>
                      <input type="email" className='w-[60%] border-2 p-2 border-gray-900' placeholder='Email'/>
                      <input type="text" className='w-[60%] border-2 p-2 border-gray-900'  placeholder='Password'/>
                      <input type="text" className='w-[60%] border-2 p-2 border-gray-900' placeholder='Confirm password'/>
                      <button className='bg-sky-700 w-[40%] text-white rounded-md p-2 font-bold' type="submit" name="submit" value="submit">Submit</button>
                      </div>

                      </Form>

                  )

                  }
       
        </Formik>

```


We can call our onSubmit={handleSubmit} on the Form tag, the onChange={handleChange} on each individual inputs and onBlur={handleBlur}.

Then we pass an onSubmit to our Formik tag with a function that alert the values just to be sure we are on the right track.

```
<Formik
              initialValues={{
                username: "",
                email: "",
                password: "",
confirm: "",
              }}
              validationSchema={schema}
              onSubmit={(values)=> {
                alert(values)
              }}
              
              >

                  {({handleChange, handleSubmit, handleBlur, touched, errors})=> (

                      <Form className='w-full h-full' onSubmit={handleSubmit}>
                            
                      <div className='flex flex-col gap-2 items-center justify-center h-full'>
                      <div className='font-bold text-xl'>Validation</div> 
                      <input onChange={handleChange} onBlur={handleBlur} className='w-[60%] border-2 p-2 border-gray-900' type="text"  placeholder='Name'/>
                      <input  onChange={handleChange} onBlur={handleBlur} type="email" className='w-[60%] border-2 p-2 border-gray-900' placeholder='Email'/>
                      <input onChange={handleChange} onBlur={handleBlur}  type="password" className='w-[60%] border-2 p-2 border-gray-900'  placeholder='Password'/>
                      <input onChange={handleChange} onBlur={handleBlur}  type="password" className='w-[60%] border-2 p-2 border-gray-900' placeholder='Confirm password'/>
                      <button  className='bg-sky-700 w-[40%] text-white rounded-md p-2 font-bold' type="submit" name="submit" value="submit">Submit</button>
                      </div>

                      </Form>

                  )

                  }
       
        </Formik>

```

![Screenshot (68).png](https://files.peakd.com/file/peakd-hive/marvel1206/243Lvi4qsVa5Hw6kct4ZJa34WByB7K56hSpvGV3CHFqaFziUzm6Y91k8vhAYm2EGc1Jis.png)



***Unfortunately, we encounter an error***. It is due to the fact that we forgot to pass the name attribute to our inputs. Very Important. Also, set password type to password
instead of text. The names of the inputs need to match the **names** in our **initialValues**.


![Screenshot (69).png](https://files.peakd.com/file/peakd-hive/marvel1206/242rke3HTCNcgftARnk9c7GrBKHuxxo5t1uMuDbUq58yBRqiEDjkED3iadQWsAfM7nqbB.png)

Now it works, We can already define whatever we want to do with our values on successfull validation but how about letting the user know which input field they have filled incorrectly? We can do this by creating a little div below each of our inputs that checks if the input has been touched before displaying the errors.inputname. {touched && touched.username && errors.username}

 ```
<Form className='w-full h-full' onSubmit={handleSubmit}>
                        
                      <div className='flex flex-col gap-2 items-center justify-center h-full'>
                      <div className='font-bold text-xl'>Validation</div> 
                      <input onChange={handleChange} onBlur={handleBlur}  name="username" className='w-[60%] border-2 p-2 border-gray-900' type="text"  placeholder='Name'/>
                      <div className='text-red-500 text-[10px]'>
                      {touched && touched.email &&errors.username}
                     </div> 
                      <input   onChange={handleChange} onBlur={handleBlur}  name='email' type="email" className='w-[60%] border-2 p-2 border-gray-900' placeholder='Email'/>
                      <div className='text-red-500 text-[10px]'>
                      {touched && touched.email &&errors.email}
                     </div> 
                      <input  onChange={handleChange} onBlur={handleBlur} name='password'  type="password" className='w-[60%] border-2 p-2 border-gray-900'  placeholder='Password'/>
                      <div className='text-red-500 text-[10px]'>
                      {touched && touched.email &&errors.password}
                     </div> 
                      <input  onChange={handleChange} onBlur={handleBlur} name='confirm' type="password" className='w-[60%] border-2 p-2 border-gray-900' placeholder='Confirm password'/>
                      <div className='text-red-500 text-[10px]'>
                      {touched && touched.email &&errors.confirm}
                     </div> 
                      <button  className='bg-sky-700 w-[40%] text-white rounded-md p-2 font-bold' type="submit" value="submit">Submit</button>
                      </div>

                      </Form>

```


![IMG_20221004_224329.jpg](https://files.peakd.com/file/peakd-hive/marvel1206/23r1iyVPkujX6Q89nrvwxNGyTtmtRy89K2iHWK3RSv8jRGJtjPEqUUMP6Ht8hQL4aGQxg.jpg)

And just like that we have created a standard but basic validated form. Of course, it can get more complex than this but that is totally up to you. Their documentation [**here**](https://www.npmjs.com/package/yup#api) can help with all you need. Also, worthy to note that the button will not submit if the user clicks when the errors object is not empty.


###### PS: Links to these modules have been provided in text. Cover photo was designed on Canva and all other images are screenshots by me.

Follow for more♥️
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 362 others
👎  
properties (23)
authormarvel1206
permlinkhow-to-easily-validate-forms
categoryhive-169321
json_metadata"{"links":["https://www.npmjs.com/package/yup","https://www.npmjs.com/package/formik","https://tailwindcss.com/docs/installation","https://www.npmjs.com/package/yup#api"],"image":["https://files.peakd.com/file/peakd-hive/marvel1206/48aZBq9ovryQePNznxhyMtufJpU5nbykQUAuNeMknfWRQxLDPs2JRvHNAm73cuwDx2.jpg","https://files.peakd.com/file/peakd-hive/marvel1206/Enz2yFsKn4AFpbWwtCx2JwzXcN5pbGRqf73gHUBjynSBbeLZLYPBDPSKmDKtMVHY2vw.jpg","https://files.peakd.com/file/peakd-hive/marvel1206/243Lvi4qsVa5Hw6kct4ZJa34WByB7K56hSpvGV3CHFqaFziUzm6Y91k8vhAYm2EGc1Jis.png","https://files.peakd.com/file/peakd-hive/marvel1206/242rke3HTCNcgftARnk9c7GrBKHuxxo5t1uMuDbUq58yBRqiEDjkED3iadQWsAfM7nqbB.png","https://files.peakd.com/file/peakd-hive/marvel1206/23r1iyVPkujX6Q89nrvwxNGyTtmtRy89K2iHWK3RSv8jRGJtjPEqUUMP6Ht8hQL4aGQxg.jpg"],"thumbnails":["https://files.peakd.com/file/peakd-hive/marvel1206/48aZBq9ovryQePNznxhyMtufJpU5nbykQUAuNeMknfWRQxLDPs2JRvHNAm73cuwDx2.jpg","https://files.peakd.com/file/peakd-hive/marvel1206/Enz2yFsKn4AFpbWwtCx2JwzXcN5pbGRqf73gHUBjynSBbeLZLYPBDPSKmDKtMVHY2vw.jpg","https://files.peakd.com/file/peakd-hive/marvel1206/243Lvi4qsVa5Hw6kct4ZJa34WByB7K56hSpvGV3CHFqaFziUzm6Y91k8vhAYm2EGc1Jis.png","https://files.peakd.com/file/peakd-hive/marvel1206/242rke3HTCNcgftARnk9c7GrBKHuxxo5t1uMuDbUq58yBRqiEDjkED3iadQWsAfM7nqbB.png","https://files.peakd.com/file/peakd-hive/marvel1206/23r1iyVPkujX6Q89nrvwxNGyTtmtRy89K2iHWK3RSv8jRGJtjPEqUUMP6Ht8hQL4aGQxg.jpg"],"tags":["hive-169321","stem","programming","vyb","pob","hive-learners","javascript"],"description":"Validating your Forms with Yup and Formik in simple steps","app":"ecency/3.0.28-vision","format":"markdown+html"}"
created2022-10-04 22:08:09
last_update2022-10-04 22:19:54
depth0
children2
last_payout2022-10-11 22:08:09
cashout_time1969-12-31 23:59:59
total_payout_value12.932 HBD
curator_payout_value12.812 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length14,687
author_reputation31,337,183,854,857
root_title"How to Easily Validate Forms with Yup and Formik"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id117,190,806
net_rshares37,551,322,870,686
author_curate_reward""
vote details (427)
@poshtoken ·
https://twitter.com/marvelstalwart/status/1577420783428698113
<sub> The rewards earned on this comment will go directly to the people sharing the post on Twitter as long as they are registered with @poshtoken. Sign up at https://hiveposh.com.</sub>
properties (22)
authorposhtoken
permlinkre-marvel1206-how-to-easily-validate-forms-4272
categoryhive-169321
json_metadata"{"app":"Poshtoken 0.0.1","payoutToUser":[]}"
created2022-10-04 22:14:09
last_update2022-10-04 22:14:09
depth1
children0
last_payout2022-10-11 22:14:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length249
author_reputation3,941,430,326,756,108
root_title"How to Easily Validate Forms with Yup and Formik"
beneficiaries
0.
accountreward.app
weight10,000
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id117,190,935
net_rshares0
@stemsocial ·
re-marvel1206-how-to-easily-validate-forms-20221005t045901254z
<div class='text-justify'> <div class='pull-left'>
 <img src='https://stem.openhive.network/images/stemsocialsupport7.png'> </div>

Thanks for your contribution to the <a href='/trending/hive-196387'>STEMsocial community</a>. Feel free to join us on <a href='https://discord.gg/9c7pKVD'>discord</a> to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.&nbsp;<br />&nbsp;<br />
</div>
properties (22)
authorstemsocial
permlinkre-marvel1206-how-to-easily-validate-forms-20221005t045901254z
categoryhive-169321
json_metadata{"app":"STEMsocial"}
created2022-10-05 04:59:00
last_update2022-10-05 04:59:00
depth1
children0
last_payout2022-10-12 04:59:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length565
author_reputation22,460,334,324,555
root_title"How to Easily Validate Forms with Yup and Formik"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id117,198,666
net_rshares0