<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alaribe Bright</title>
    <description>The latest articles on DEV Community by Alaribe Bright (@laribright).</description>
    <link>https://dev.to/laribright</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F587705%2F327452ec-bd7e-4b97-a28a-5417496607e8.jpeg</url>
      <title>DEV Community: Alaribe Bright</title>
      <link>https://dev.to/laribright</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/laribright"/>
    <language>en</language>
    <item>
      <title>Form Validation a breeze with Yup and React Hook form</title>
      <dc:creator>Alaribe Bright</dc:creator>
      <pubDate>Wed, 24 Aug 2022 09:18:00 +0000</pubDate>
      <link>https://dev.to/laribright/form-validation-a-breeze-with-yup-and-react-hook-form-3pde</link>
      <guid>https://dev.to/laribright/form-validation-a-breeze-with-yup-and-react-hook-form-3pde</guid>
      <description>&lt;p&gt;Hello everyone, today I'll be guiding us on how to add form validation to our React application using Yup and react hook form.&lt;/p&gt;

&lt;p&gt;Video version&lt;br&gt;
&lt;a href="https://youtu.be/chYXh7TG9ZE" rel="noopener noreferrer"&gt;Video version youtube&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Final Demo&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz6q8qxavw7a98km9sm4q.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz6q8qxavw7a98km9sm4q.gif" alt="Form validation in react" width="600" height="721"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Getting started, I've generated a new react project and setup tailwind css.&lt;br&gt;
Incase you want to add tailwind css to your react project please follow this guide &lt;a href="https://tailwindcss.com/docs/guides/create-react-app" rel="noopener noreferrer"&gt;tailwindcss-react&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is what I currently have:&lt;br&gt;
App.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  return &amp;lt;div className="w-screen h-screen bg-gradient-to-r from-blue-900 to-purple-900 grid place-content-center"&amp;gt;

  &amp;lt;/div&amp;gt;;
}

export default App;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next let's create a components folder that will hold our Form component&lt;/p&gt;

&lt;p&gt;src/components/Form/Form.jsx&lt;/p&gt;

&lt;p&gt;At this point we're going to create our form component and have our various form inputs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const textInputClassName =
  "bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500";

const Form = () =&amp;gt; {
  return (
    &amp;lt;div className="md:w-[500px] shadow-sm shadow-white bg-white w-[320px] mx-auto px-7 py-4 rounded-xl"&amp;gt;
      &amp;lt;form className="w-full"&amp;gt;
        &amp;lt;div className="mb-6"&amp;gt;
          &amp;lt;label
            htmlFor="email"
            className="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-300"
          &amp;gt;
            Your email
          &amp;lt;/label&amp;gt;
          &amp;lt;input
            type="email"
            id="email"
            className={textInputClassName}
            placeholder="test@test.com"
          /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div className="mb-6"&amp;gt;
          &amp;lt;label
            htmlFor="password"
            className="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-300"
          &amp;gt;
            Your password
          &amp;lt;/label&amp;gt;
          &amp;lt;input type="password" id="password" className={textInputClassName} /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div className="mb-6"&amp;gt;
          &amp;lt;label
            htmlFor="confirmPassword"
            className="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-300"
          &amp;gt;
            Confirm Password
          &amp;lt;/label&amp;gt;
          &amp;lt;input
            type="password"
            id="confirmPassword"
            className={textInputClassName}
          /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div className="mb-6"&amp;gt;
          &amp;lt;label
            htmlFor="accountType"
            className="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-400"
          &amp;gt;
            Select an option
          &amp;lt;/label&amp;gt;
          &amp;lt;select
            id="accountType"
            className="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
          &amp;gt;
            &amp;lt;option value=""&amp;gt;Account Type&amp;lt;/option&amp;gt;
            &amp;lt;option value="personal"&amp;gt;Personal&amp;lt;/option&amp;gt;
            &amp;lt;option value="commercial"&amp;gt;Commercial&amp;lt;/option&amp;gt;
          &amp;lt;/select&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div className="flex justify-between mb-6"&amp;gt;
          &amp;lt;div className="flex"&amp;gt;
            &amp;lt;div className="flex items-center h-5"&amp;gt;
              &amp;lt;input
                id="remember"
                type="checkbox"
                value=""
                className="w-4 h-4 bg-gray-50 rounded border border-gray-300 focus:ring-3 focus:ring-blue-300 dark:bg-gray-700 dark:border-gray-600 dark:focus:ring-blue-600 dark:ring-offset-gray-800"
              /&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;label
              htmlFor="remember"
              className="ml-2 text-sm font-medium text-gray-900 dark:text-gray-300"
            &amp;gt;
              Remember me
            &amp;lt;/label&amp;gt;
          &amp;lt;/div&amp;gt;

          &amp;lt;div&amp;gt;
            &amp;lt;label
              htmlFor="default-toggle"
              className="inline-flex relative items-center cursor-pointer"
            &amp;gt;
              &amp;lt;input
                type="checkbox"
                value=""
                id="default-toggle"
                className="sr-only peer"
              /&amp;gt;
              &amp;lt;div className="w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-blue-300 dark:peer-focus:ring-blue-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-blue-600"&amp;gt;&amp;lt;/div&amp;gt;
              &amp;lt;span className="ml-3 text-sm font-medium text-gray-900 dark:text-gray-300"&amp;gt;
                Toggle me
              &amp;lt;/span&amp;gt;
            &amp;lt;/label&amp;gt;
          &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;

        &amp;lt;button
          type="submit"
          className="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"
        &amp;gt;
          Submit
        &amp;lt;/button&amp;gt;
      &amp;lt;/form&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Form;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we're done with our Form component JSX, let's go on and add our form to App.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Form from "./components/Form/Form";

function App() {
  return (
    &amp;lt;div className="w-screen h-screen bg-gradient-to-r from-blue-900 to-purple-900 grid place-content-center"&amp;gt;
      &amp;lt;Form /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our App.js now looks like this which gives us this result&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi01ly30ion6bwewav78k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi01ly30ion6bwewav78k.png" alt="css-form" width="800" height="438"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Now we've our form design, let's proceed to adding validation. We need to install the following packages
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;npm install -D yup @hookform/resolvers react-hook-form&lt;/code&gt;&lt;br&gt;
or incase you use yarn&lt;br&gt;
&lt;code&gt;yarn add -D yup @hookform/resolvers react-hook-form&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Yup is going to be our schema builder for value parsing and validation,&lt;br&gt;
React-hook-form is going to help us validate our form input,&lt;br&gt;
@hookform/resolvers is used to integrate yup and react-hook-form nicely. &lt;/p&gt;

&lt;p&gt;Let's import the packages we just installed in our Form component&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;/components/Form/Form.jsx&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useForm } from "react-hook-form";
import * as yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before we start building our ValidationSchema, we need to add a &lt;strong&gt;name attribute&lt;/strong&gt; to our html form, as this is important for yup and react-hook-form to keep track of our different inputs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   &amp;lt;form className="w-full"&amp;gt;
        &amp;lt;div className="mb-6"&amp;gt;
          &amp;lt;label
            htmlFor="email"
            className="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-300"
          &amp;gt;
            Your email
          &amp;lt;/label&amp;gt;
          &amp;lt;input
            type="email"
            name="email"
            id="email"
            className={textInputClassName}
            placeholder="test@test.com"
          /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div className="mb-6"&amp;gt;
          &amp;lt;label
            htmlFor="password"
            className="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-300"
          &amp;gt;
            Your password
          &amp;lt;/label&amp;gt;
          &amp;lt;input type="password" id="password" className={textInputClassName} /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div className="mb-6"&amp;gt;
          &amp;lt;label
            htmlFor="confirmPassword"
            className="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-300"
          &amp;gt;
            Confirm Password
          &amp;lt;/label&amp;gt;
          &amp;lt;input
            name="password"
            type="password"
            id="confirmPassword"
            className={textInputClassName}
          /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div className="mb-6"&amp;gt;
          &amp;lt;label
            htmlFor="accountType"
            className="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-400"
          &amp;gt;
            Select an option
          &amp;lt;/label&amp;gt;
          &amp;lt;select
            name="accountType"
            id="accountType"
            className="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
          &amp;gt;
            &amp;lt;option value=""&amp;gt;Account Type&amp;lt;/option&amp;gt;
            &amp;lt;option value="personal"&amp;gt;Personal&amp;lt;/option&amp;gt;
            &amp;lt;option value="commercial"&amp;gt;Commercial&amp;lt;/option&amp;gt;
          &amp;lt;/select&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div className="flex justify-between mb-6"&amp;gt;
          &amp;lt;div className="flex"&amp;gt;
            &amp;lt;div className="flex items-center h-5"&amp;gt;
              &amp;lt;input
                id="remember"
                name="remember"
                type="checkbox"
                value=""
                className="w-4 h-4 bg-gray-50 rounded border border-gray-300 focus:ring-3 focus:ring-blue-300 dark:bg-gray-700 dark:border-gray-600 dark:focus:ring-blue-600 dark:ring-offset-gray-800"
              /&amp;gt;
            &amp;lt;/div&amp;gt;
            &amp;lt;label
              htmlFor="remember"
              className="ml-2 text-sm font-medium text-gray-900 dark:text-gray-300"
            &amp;gt;
              Remember me
            &amp;lt;/label&amp;gt;
          &amp;lt;/div&amp;gt;

          &amp;lt;div&amp;gt;
            &amp;lt;label
              htmlFor="toggle"
              className="inline-flex relative items-center cursor-pointer"
            &amp;gt;
              &amp;lt;input
                type="checkbox"
                name="toggle"
                value=""
                id="toggle"
                className="sr-only peer"
              /&amp;gt;
              &amp;lt;div className="w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-blue-300 dark:peer-focus:ring-blue-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-blue-600"&amp;gt;&amp;lt;/div&amp;gt;
              &amp;lt;span className="ml-3 text-sm font-medium text-gray-900 dark:text-gray-300"&amp;gt;
                Accept
              &amp;lt;/span&amp;gt;
            &amp;lt;/label&amp;gt;
          &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;

        &amp;lt;button
          type="submit"
          className="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"
        &amp;gt;
          Submit
        &amp;lt;/button&amp;gt;
      &amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's build our validation schema, for this I'll create a new schema folder and inside a formSchema.js file.&lt;/p&gt;

&lt;p&gt;let's write our formSchema, like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as yup from "yup";

export const registerSchema = yup.object().shape({
  email: yup
    .string("email should be a string")
    .email("please provide a valid email address")
    .required("email address is required"),
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The email key should match the name attribute in your jsx.&lt;/p&gt;

&lt;p&gt;In our Form.js&lt;br&gt;
&lt;code&gt;import { registerSchema } from "../../schema/formSchema";&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Saving space

const Form = () =&amp;gt; {
  const {
    register,
    handleSubmit,
    formState: { errors },
  } = useForm({
    resolver: yupResolver(registerSchema),
  });

// Saving space
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;register will be used to register our inputs with react-hook-form,&lt;br&gt;
handleSubmit should be added to our form onSubmit and when we submit our form, will help use validate our form,&lt;br&gt;
formState helps us keep track of our form state, in this case error state.&lt;/p&gt;

&lt;p&gt;let's add this to our email input, please take note of &lt;code&gt;{...register("email")}&lt;/code&gt; and the error jsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;label
            htmlFor="email"
            className="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-300"
          &amp;gt;
            Your email
          &amp;lt;/label&amp;gt;
          &amp;lt;input
            {...register("email")}
            type="email"
            name="email"
            id="email"
            className={textInputClassName}
            placeholder="test@test.com"
          /&amp;gt;
          {errors.email ? (
            &amp;lt;span className="text-red-900"&amp;gt;{errors.email.message}&amp;lt;/span&amp;gt;
          ) : (
            &amp;lt;&amp;gt;&amp;lt;/&amp;gt;
          )}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;on our form submit handler, let's add this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form onSubmit={handleSubmit(formSubmitHandler)} className="w-full"&amp;gt;
// saving space
&amp;lt;/from
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll notice we've passed formSubmitHandler which is our custom function that will be passed the form data automatically if validation passes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const formSubmitHandler = (data) =&amp;gt; {
    console.log(data);
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this our form validation is working already and we should have a result like this&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftmhh65yks81vvecexuoc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftmhh65yks81vvecexuoc.gif" alt="Email validation" width="450" height="282"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;validating password and confirm password&lt;/p&gt;

&lt;p&gt;Let's add the following to our schema file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const registerSchema = yup.object().shape({
  email: yup
    .string("email should be a string")
    .email("please provide a valid email address")
    .required("email address is required"),
  password: yup
    .string("password should be a string")
    .min(5, "password should have a minimum length of 5")
    .max(12, "password should have a maximum length of 12")
    .required("password is required"),
  confirmPassword: yup
    .string("password should be a string")
    .oneOf([yup.ref("password")])
    .required("confirm password is required"),
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to our Form.js, let's update our password and confirm password to this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div className="mb-6"&amp;gt;
          &amp;lt;label
            htmlFor="password"
            className="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-300"
          &amp;gt;
            Your password
          &amp;lt;/label&amp;gt;
          &amp;lt;input
            {...register("password")}
            type="password"
            name="password"
            id="password"
            className={textInputClassName}
          /&amp;gt;
          {errors.password ? (
            &amp;lt;span className="text-red-900"&amp;gt;{errors.password.message}&amp;lt;/span&amp;gt;
          ) : (
            &amp;lt;&amp;gt;&amp;lt;/&amp;gt;
          )}
        &amp;lt;/div&amp;gt;
        &amp;lt;div className="mb-6"&amp;gt;
          &amp;lt;label
            htmlFor="confirmPassword"
            className="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-300"
          &amp;gt;
            Confirm Password
          &amp;lt;/label&amp;gt;
          &amp;lt;input
            {...register("confirmPassword")}
            name="confirmPassword"
            type="password"
            id="confirmPassword"
            className={textInputClassName}
          /&amp;gt;
          {errors.confirmPassword ? (
            &amp;lt;span className="text-red-900"&amp;gt;{errors.confirmPassword.message}&amp;lt;/span&amp;gt;
          ) : (
            &amp;lt;&amp;gt;&amp;lt;/&amp;gt;
          )}
        &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives us this result&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnul28rqglx8q2eqrtnhc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnul28rqglx8q2eqrtnhc.gif" alt="Password and confirm password" width="600" height="627"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Validating Select&lt;/p&gt;

&lt;p&gt;Let's update our schema file to the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as yup from "yup";

export const registerSchema = yup.object().shape({
  email: yup
    .string("email should be a string")
    .email("please provide a valid email address")
    .required("email address is required"),
  password: yup
    .string("password should be a string")
    .min(5, "password should have a minimum length of 5")
    .max(12, "password should have a maximum length of 12")
    .required("password is required"),
  confirmPassword: yup
    .string("password should be a string")
    .oneOf([yup.ref("password")])
    .required("confirm password is required"),
  accountType: yup
    .string("account type should be a string")
    .oneOf(["personal", "commercial"])
    .required("account type is required"),
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's also update our select jsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div className="mb-6"&amp;gt;
          &amp;lt;label
            htmlFor="accountType"
            className="block mb-2 text-sm font-medium text-gray-900 dark:text-gray-400"
          &amp;gt;
            Select an option
          &amp;lt;/label&amp;gt;
          &amp;lt;select
            {...register("accountType")}
            name="accountType"
            id="accountType"
            className="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
          &amp;gt;
            &amp;lt;option value=""&amp;gt;Account Type&amp;lt;/option&amp;gt;
            &amp;lt;option value="personal"&amp;gt;Personal&amp;lt;/option&amp;gt;
            &amp;lt;option value="commercial"&amp;gt;Commercial&amp;lt;/option&amp;gt;
          &amp;lt;/select&amp;gt;{" "}
          {errors.accountType ? (
            &amp;lt;span className="text-red-900"&amp;gt;{errors.accountType.message}&amp;lt;/span&amp;gt;
          ) : (
            &amp;lt;&amp;gt;&amp;lt;/&amp;gt;
          )}
        &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we've this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3lv19urbf3qm8c9qr406.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3lv19urbf3qm8c9qr406.gif" alt="Select validation" width="600" height="700"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lastly, let's validate our toggle and checkbox&lt;/p&gt;

&lt;p&gt;We start by updating our schema file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import * as yup from "yup";

export const registerSchema = yup.object().shape({
  email: yup
    .string("email should be a string")
    .email("please provide a valid email address")
    .required("email address is required"),
  password: yup
    .string("password should be a string")
    .min(5, "password should have a minimum length of 5")
    .max(12, "password should have a maximum length of 12")
    .required("password is required"),
  confirmPassword: yup
    .string("password should be a string")
    .oneOf([yup.ref("password")])
    .required("confirm password is required"),
  accountType: yup
    .string("account type should be a string")
    .oneOf(["personal", "commercial"])
    .required("account type is required"),
  remember: yup.boolean().oneOf([true], "Please tick checkbox"),
  toggle: yup.boolean().oneOf([true], "Please toggle accept"),
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then let's update our from checkbox and toggle jsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div className="flex justify-between mb-6"&amp;gt;
          &amp;lt;div&amp;gt;
            &amp;lt;div className="flex"&amp;gt;
              &amp;lt;div className="flex items-center h-5"&amp;gt;
                &amp;lt;input
                  {...register("remember")}
                  id="remember"
                  name="remember"
                  type="checkbox"
                  value=""
                  className="w-4 h-4 bg-gray-50 rounded border border-gray-300 focus:ring-3 focus:ring-blue-300 dark:bg-gray-700 dark:border-gray-600 dark:focus:ring-blue-600 dark:ring-offset-gray-800"
                /&amp;gt;
              &amp;lt;/div&amp;gt;
              &amp;lt;label
                htmlFor="remember"
                className="ml-2 text-sm font-medium text-gray-900 dark:text-gray-300"
              &amp;gt;
                Remember me
              &amp;lt;/label&amp;gt;
            &amp;lt;/div&amp;gt;
            {errors.remember ? (
              &amp;lt;span className="text-red-900"&amp;gt;{errors.remember.message}&amp;lt;/span&amp;gt;
            ) : (
              &amp;lt;&amp;gt;&amp;lt;/&amp;gt;
            )}
          &amp;lt;/div&amp;gt;

          &amp;lt;div&amp;gt;
            &amp;lt;div&amp;gt;
              &amp;lt;label
                htmlFor="toggle"
                className="inline-flex relative items-center cursor-pointer"
              &amp;gt;
                &amp;lt;input
                  {...register("toggle")}
                  type="checkbox"
                  name="toggle"
                  value=""
                  id="toggle"
                  className="sr-only peer"
                /&amp;gt;
                &amp;lt;div className="w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-blue-300 dark:peer-focus:ring-blue-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-blue-600"&amp;gt;&amp;lt;/div&amp;gt;
                &amp;lt;span className="ml-3 text-sm font-medium text-gray-900 dark:text-gray-300"&amp;gt;
                  Accept
                &amp;lt;/span&amp;gt;
              &amp;lt;/label&amp;gt;
            &amp;lt;/div&amp;gt;
            {errors.toggle ? (
              &amp;lt;span className="text-red-900"&amp;gt;{errors.toggle.message}&amp;lt;/span&amp;gt;
            ) : (
              &amp;lt;&amp;gt;&amp;lt;/&amp;gt;
            )}
          &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whooaa with this, we're done with this result&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foy0ambn9s2qxu0tj3ap4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foy0ambn9s2qxu0tj3ap4.gif" alt="validating checkbox and toggle" width="600" height="727"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for following, incase you need the final code, here's the github repo&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/laribright/yup-react-hook-from.git" rel="noopener noreferrer"&gt;github repo link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please do connect with me&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/laribright/" rel="noopener noreferrer"&gt;linkedIn&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/channel/UCTvF1UElM6k1LrLV7u4prow" rel="noopener noreferrer"&gt;youtube&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.udemy.com/user/alaribe-bright/" rel="noopener noreferrer"&gt;Udemy&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
