Object Oriented PHP #14 - Validation Class (part 3)

Net Ninja · Beginner ·🛠️ AI Tools & Apps ·6y ago
Skills: API Design50%

Key Takeaways

Completes a form validation class in PHP and tests it with error feedback

Full Transcript

or rather gang so we're nearly at the finish line now we just need to do a few more things but now we have our user validator class and all we need to do is create a new instance of this class after a user submits the form and pass in the post data which they entered into these fields and then it can do all the validation it can spit back any errors if there are any and then we can output those to the user in the screen if there are any so let's go back to index dot PHP and first of all right here this is where we need to validate the entries so let's delete that echo statement first of all and instead create a new instance of the user validator class now before I do that I need to use that class inside this file so I need to then say at the top or require and we're going to require user underscore validator dot PHP right so now we can use that class inside this file so down here I can create a new instance of that class and I'll call this a validation and set it equal to new user validator okay so now we need to pass in the post data to this which is just dollar sign underscore and post like so that is an array and it's going to contain keys which should be username and email ok so now we need to call this method this method right here validate form because that's what kicks everything off we check the fields to see if they exist and if you do we call these two methods down here to check the individual fields then we return any areas at the end so first of all let's say errors because that's what's ultimately returned to us let's not do this in capitals errors is equal to validation which is our instance and then we'll invoke the validate form method and by the way I just want to say at this point and it's a funny point to say it I should have said it at the start there's many many ways to do this kind of validation I'm just doing one particular way here right so if you are a hundred programmers to do this you'll probably get at least about 75 different ways probably a hundred different ways of doing this so don't feel like this is all set in stone and this is exactly how you should be doing it I'm just showing you an example of creating a class and using it right here and there are many ways of doing this but anyway I digress let's carry on with this we're calling this method which is ultimately going to return us an array of errors that could be empty if there's no errors but we'll still store that empty array right here now at this point if the errors were empty this is where we'd probably save data to a database right if that's what we're doing with it now if there are errors then instead what we're going to do is output them to the form I'm not going to do this check right here and save anything to the database that's not what this course is all about we're just focused on our class and the form at the minute so instead what I'm going to do is I'm going to check if there's any errors down here inside the actual template and then output them all right so before I do that what I want to do is save this and just refresh over here then I'm going to continue and I'm going to now do all this stuff right here and okay this happened without any errors but check this out if I now go to this form down here and change this to emails for example and save it I'm going to refresh over here I'm going to continue and then if I try to enter some stuff in now we get an error it says email is not present in the data so that was this error before right here so remember when we checked each of the fields and to check that those fields right here exist right now I say actually it can't find this field in the post data because now we don't have that field inside our form it's now emails and it should be email so if we change that back then we're not going to get any error if I submit this now then it sure work okay we need to actually reload the page because that's there now but anyway if I do it now then we don't get that error okay so the next thing to do is actually output any errors if there are any now I'm going to do a separate div under each of these input fields and this is actually going to have a class equal to error so we can style this in a second just color it red or coral or something in a minute then we need to echo something out so PHP tags first of all PHP closed that off and we're going to echo and we're going to use the null coalesce operator to do this so if something has a value it outputs that if it doesn't have a value it outputs something else so we're gonna check if errors and then username has a value now if there is an error with that key inside that errors array then it will help put it then we do double question mark and then this is the alternate value double output if this is null so if that error doesn't exist then when I actually output in anything just an empty string right again you can learn more about this norco let's operate it in my PHP for beginners series anyway so now we need to duplicate that dude and paste it downhill and this time we want to check the email one all right so if we save this now all let's check this out so if I submit now we can see these two things right here username cannot be empty an email cannot be empty if I change this to something like SG then it says username must be six to twelve characters and alpha numeric so it's no longer empty but we're getting this different error and the same for this one if I enter something that's not a valid email it says email must be a valid email so these are working the thing is we're not persisting the data that a user types into these fields so if I submit something like that and it's wrong even though it's wrong or rather that wasn't but if I submit something that's wrong like that I want that to remain here when the page reloads so a user doesn't have to type out the whole thing again so to do that we need to go to the value properties of these input fields so what I'm going to do is say value is equal to something and then we're going to output the actual data in the post array right because that's what a user actually submitted so let's do our PHP tags now we have to surround whatever we output with the function HTML special cars because that is going to make our code more secure so that if a user tries to input some kind of JavaScript or something like that it's not actually going to run that a script okay so that's what this function kind of does so let's echo HTML special chars again this is all discussed in my beginners series and then inside this we need to pass through the post data so dollar sign underscore post and then the value we want is the username from that okay so now we're outputting this data right here as the value when it reloads now we need to actually do a null coalesce operator here as well because the first time the page loads this is not going to exist and it's going to output some kind of error so instead we doing null coalesce operator so if it doesn't exist then we just output a simple blank string instead so what I'm going to do is actually grab this thing right here and then down on the other input field I'm going to say value is equal to we need our PHP tags again close that off I'm going to paste this dude right in here and this time I need the email instead oops can I spell no it's been a long day so I'm saved that now and if I are refresh I'm gonna see this right here so if I type something else in now and something else down here refresh I do get the errors this is valid by the way but I do get the errors but I still get this data inside the input field so it still shows now if I type something not valid up here we get the error but this still shows awesome so that is just about in the friends there's one more thing to do and that's just to add this tiny lip style down here so we're grabbing the error class and we're cloning it coral save it and let's go over here do this again and now we can see no we don't let's have refresh and now we can see this error is in coral so it difficult so what my friends we have reached the finish line now we have successfully created this user validator class and okay it's only checking two fields but you could extend this class if you wanted to you could make additional fields for different things like a password and then you could make sure that two password fields are the same or something like that if you do that if you extend it leave your links down below so we can all check it out but there we go that is this series well and truly over so hopefully I've taught you a little bit along the way about object-oriented programming in PHP and hopefully as well you can see the benefits of using classes in your code if you've enjoyed this series guys please do not forget to share subscribe and like that really means a lot and I'm going to see you in the very next series [Music]

Original Description

Hey gang, in this final PHP OOP tutorial we'll finish off our form validation class, test it out & then show errors / feedback to the user based on their input. ---------------------------------------- 🐱‍💻 🐱‍💻 Course Links: Course files - https://github.com/iamshaunjp/object-oriented-php 🐱‍💻 🐱‍💻 Other Related Courses: + PHP Tutorial for Beginners - https://www.youtube.com/playlist?list=PL4cUxeGkcC9gksOX3Kd9KPo-O68ncT05o
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

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →