React Dynamic Form Tutorial | react-hook-form & zod

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 พ.ค. 2024
  • In this tutorial you will learn how to build a dynamic React.js form using react-hook-form, zod, and TypeScript.
    CLONE REPOSITORY github.com/AustinShelby/Dynam...
    We will be using the discriminated union validator by zod to build a dynamic form validation schema.
    You will learn how to use that custom validation schema with react-hook-form to render dynamic inputs based on form state.
    Timestamps:
    00:00 Intro
    00:40 Form validation schema
    02:44 react-hook-form
    04:34 Dynamic field
    06:24 Submitting form
    08:24 Outro
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 23

  • @athefirstletteraofthealpha769
    @athefirstletteraofthealpha769 ปีที่แล้ว

    Thank you, Austin. Your tutorial is very clear and helpful.

  • @koreandude
    @koreandude ปีที่แล้ว

    Appreciate the content Austin, subbed and liked! Hope you can continue to create more videos like this

  • @animanamit7543
    @animanamit7543 ปีที่แล้ว +3

    Great tutorial!! Your videos are really helpful while I'm learning to use Zod :)

    • @AustinShelby
      @AustinShelby  ปีที่แล้ว

      Great to hear that Animan!

  • @socialultra7219
    @socialultra7219 4 หลายเดือนก่อน

    cheers dude, I was struggling on this problem for a while, your content helped immensely

    • @AustinShelby
      @AustinShelby  4 หลายเดือนก่อน

      Glat to have helped you solve your problem!

  • @johnc0de
    @johnc0de ปีที่แล้ว

    Amazing! Thank you for the content!

  • @tosheen20
    @tosheen20 ปีที่แล้ว +5

    This is a great content, well done. Could you create a slightly complex examle. Lets say there is a drop down and first validation rule is that an option must be selected, it can not be null which is a first option. Depending on a selection different fields are displayed according to some logic. If for example email is selected from the drop down additional email field is displayed and it must be valid email. If telephone is selected then it must be valid phone number etc. I am curious to know how will zod schema look in that case because we have default null state for the drop down and in the case we have more drop downs with different "branching". Thank you.

  • @LuisLarreaJr
    @LuisLarreaJr 2 หลายเดือนก่อน +1

    Great example. I had something similar when product had the bright idea to still show the fields but disabled. Apparently this really complicates things since now you have to disable the validation rules from triggering on disabled fields.

  • @arturpawelski321
    @arturpawelski321 ปีที่แล้ว

    thank you for help ,you are great teacher❤

  • @VinayKumarSupervisor
    @VinayKumarSupervisor 11 หลายเดือนก่อน

    Clean !😀... Thanks

  • @acubesa
    @acubesa 8 หลายเดือนก่อน

    This is great, but what if the form has over 20 fields, I would need to create a copy of it for every version, is there a more 'versatile' way of achieving this? Maybe when setting the definition of a type in the schema, can I co-require a field, instead of creating different possible versions of it? I'm new to zod as you can tell...

    • @acubesa
      @acubesa 8 หลายเดือนก่อน

      Nevermind, I found a more dynamic way to do it, just creating the z.object with the diff, and merging the base schema, so I don't have to create copies for every version.

    • @AustinShelby
      @AustinShelby  8 หลายเดือนก่อน +1

      @@acubesa I'm glad you found the solution for it and thanks for sharing it with everyone 👍

  • @miketepes8208
    @miketepes8208 ปีที่แล้ว

    Thank you very much Sir. I learned a lot. I applied this to checkout page where checkbox is biling address is same with shipping address or not. Thank you very much

    • @AustinShelby
      @AustinShelby  ปีที่แล้ว

      Hey Mike it's awesome to hear that this video helped you! Thanks for watching.

  • @HariKrishna-qh5zp
    @HariKrishna-qh5zp ปีที่แล้ว +2

    its a great tutorial Austin, however I strongly feel you must have adopted some design library as the implementation of Tailwind CSS made this look so clumsy and not on the point to what the video trying to convey. I mostly prefer MUI or Chakra UI for simple and elegant design of forms.

    • @AustinShelby
      @AustinShelby  ปีที่แล้ว +1

      Thanks for watching Hari. While UI libraries like Material UI are useful for creating good looking designs quickly, they cause some extra complexity when integrating form libraries such as react-hook-form.

    • @HariKrishna-qh5zp
      @HariKrishna-qh5zp ปีที่แล้ว

      @@AustinShelby Thanks so much for your response. You are absolutely right. While I was working on a dynamic form similar to the one in the video, I had to disable form fields after checkbox however @Mui latest version's Checkbox is not allowing that functionality.

  • @rastaray6401
    @rastaray6401 ปีที่แล้ว +1

    How do you access errors on the email field? Accessing formstate.errors.email results in a "property does not exist" TypeScript error

    • @AustinShelby
      @AustinShelby  ปีที่แล้ว +3

      TypeScript is correct in this case. If the checkbox is not checked there won't be a property email in the formState.errors object. To fix this, you have to first check if the object has the email key like this:
      "email" in formState.errors && formState.errors.email

  • @Foused87
    @Foused87 ปีที่แล้ว

    Why you stopped making videos? Your videos are so helpful!

    • @AustinShelby
      @AustinShelby  ปีที่แล้ว

      Honestly just pure laziness. I should just probably get back to it as people seem to be enjoying these videos.