React Native Tutorial #32 - Validation with Yup

Net Ninja · Beginner ·🌐 Frontend Engineering ·6y ago

Key Takeaways

Validates forms using Yup in React Native

Full Transcript

okay that my friends so currently when we go to add a new review we don't have any kind of validation we could type anything or nothing in these and it's still gonna accept that now this is not good we need some kind of validation now to add this we could spend a while writing our own validation functions then calling those functions when the form submits and formulating error messages to show etc etc but that could be a bit lengthy and take a fair amount of logic and code and instead I'll be using a package called New York to help us with this invalidation and it also works really well with forming so the first thing we need to do is install this package so let's go to our other terminal and I'm gonna say NPM install yup and press ENTER so the way that this is going to work is that we're going to create a validation schema for our review form using Europe and then we're going to pass that schema into our form ik components now a schema is just a set of rules defined inside an object and that object is created by your forest now the keys inside that object they are going to be the different form fields the title the body and the rating and the values to those keys are the validation rules what does a value have to do to pass that validation and then when we submit the form in the future because we'll link our schema with this format component when we submit it it's going to look at those values and try and match against our schema to make sure everything passes and only carry on with this submission if everything does pass and it doesn't then we can return some kind of error so in this video we're going to focus on creating this schema and passing it in to formic now the first thing we need to do is import your at the top so to do this we'll say import asterix so that's everything as your from Europe okay now we can create our review schema which I'm going to do outside of the review form component it doesn't need to be in there soaked honest review schema is going to be equal to your dot object this is a method that creates us a yup object and inside here we define our schema so remember I said the keys of the different key value pairs inside the schema are going to be the different form fields so we have a title a body and a rating so let's do the title first of all that's the first key and then the value is going to be some kind of validation rule now to create this validation rule we say yup and then first of all I'm going to say string and that means that it must be a string so if title is not a string then it's going to fail now we can also add on another method and that is going to be required and that means that this field is required so if we don't enter anything then it's going to fail validation and I'm gonna tack on another method called mint and I'm gonna pass in for and this means that the minimum length of this string field must be 4 characters so anything less and it's going to fail validation so that's pretty easy to create those validation rules rather than creating a function to do all that stuff ourselves now the way I'm going to write these different validation rules is by going on to a new line for every little rule so we have first of all a string they're then required then min and that's so we don't get a long chain of rules that goes off the screen later on but anyway that is our title pretty much done so we can do the next field now which is going to be the body and again we say Europe this is going to be a string so dot string and then we're gonna say this is also required so don't require it and again I'm going to use min to say the minimum length of this is going to be 8 now in reality it might be something more but I really don't want to start typing out link the articles in this tutorial so I've just kept it at 8 for now and that will do for the body so it has to be a string it's required so it can't be blank and minimum length of a characters so the rating is gonna be slightly more difficult because for the rating we want a number between 1 and 5 so let's do this first of all rating yup and then I'm actually going to say string again because by default when we type something into an input field it is automatically a string and not a number so that's fine it's also required so it required like so and then down here I'm going to use a method called test now the test method allows us to define a function and that function is going to take in the value the rating value as a parameter and we will run a little test on that function and return either true or false so we can check that the value is actually within certain confines if it returns true it means that this field is valid if it's false it means it's not valid so inside this test method we define three things first of all some kind of name for the test so it's going to be is num1 so 5 you can call up what you want and the second parameter is also string and this is any kind of feedback message that you want to provide to a user if this test fails so I'm gonna say rating must be a number 1 to 5 okay so if it fails we're going to eventually provide that validation error to the user and the third parameter is the function which takes in the value of the rating and inside this function we can return either true or false I'm going to return a statement which is pass int and we pass in the value so first of all we're passing in a string which could be 5 still and we're passing that into an integer right and I'm gonna check is that less than 6 because a rating can go up to 5 only but that's not the only thing I want to check so ampersand double ampersand to check something else as well and pass int passing the value again that must also be greater than zero okay so only if the rating is an integer between 1 and 5 is this gonna return true if it doesn't return any of those it's going to return false so then it fails the validation okay so that is pretty much it we have done now I won't review schema for the three different fields and we can just pass this into our formic forum so we do that down here inside the formic components and all we do is use a prop called validation schema and this accepts the review schema like so so when we submit our form it's going to look at the validation schema and it's gonna match it against our different fields so we have a title field a body field and a rating field and we have a title validation a body validation and also a rating validation so it's gonna look at those and try and match our values against them if the values pass all of the validation then it's going to continue with the submission and it's going to run this function if it doesn't then it won't it won't let us fully submit the form okay and instead what it's going to do is populate some errors in the farming props and we'll see that in the next video but for now what I'm gonna do is save this but before I do I've just seen that this thing up here still grayed out and that's because it has a capital I don't want this to have a capital because we've not given it a capital down there it doesn't really matter whether you do I don't but just make sure they're the same I'm gonna save that now and what I need to do is restart the current process because we installed a new package so let me cancel out of this and run x-bow starts again I'm going to also cancel this up over here and then in my new developer tools I'm going to run on an android device and hopefully we should see the updated app over here okay so if I try now to add a new item I'm just gonna try to submit as is and it doesn't let me do that it did let me do that before if I type in just two letters here and one there and submit again doesn't let me do it now if we type in five letters here which is over four it passes the validation and I do a load of letters here and I also do a number here submit it now it does let me do it so it's only letting me fully submit the form if our values pass the validation schema we do mind right here okay so that's how easy it is to implement some validation with your inside a formic forum now in the next video we're going to see how to actually give some feedback to a user because if we try to submit our form and it runs through the schema and it decides it's not valid then your is going to provide some error messages to pharma-quick attached to these props so we can then output those error messages to the user so they can correct what they've done wrong so we'll take a look at how to do that in the next video

Original Description

Hey gang, in this React Native tutorial I'll show you how to validate your forms easily with a 3rd party package called Yup. ---------------------------------------- 🐱‍💻 🐱‍💻 Course Links: Course files - https://github.com/iamshaunjp/react-native-tutorial 🐱‍💻 🐱‍💻 Other Related Courses: + Complete React Tutorial - https://www.youtube.com/watch?v=OxIDLw0M-m0&list=PL4cUxeGkcC9ij8CfkAY2RAGb-tmkNwQHG + React Hooks & Context Tutorial - https://www.youtube.com/watch?v=6RhOzQciVwI&list=PL4cUxeGkcC9hNokByJilPg5g9m2APUePI + Modern JavaScript Tutorial - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Prepare for a frontend developer interview with Capgemini by reviewing JavaScript fundamentals and practicing common interview questions
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with 10 essential tools for modern web app development
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with top 10 developer tools in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
US frontend engineer hiring demand peaked in 2022 and remains flat-depressed in 2026, contrary to common assumptions
Dev.to AI
Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →