Basic Form Validation In Laravel 8 | Laravel 8 For Beginners | Learn Laravel 8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys my name is dari and i hope that you have a great day because today we're going to talk about how to validate the incoming data in larval because of you guys i've reached something very special that i never ever dreamed about reaching in one year this channel got over 5k subscribers in 2020 and i can't thank every single person that watches my video enough i want to do a simple 50 amazon gift card giveaway and there's not a lot that you need to do i recently created an instagram account which i have linked in the description down below and this is not a sponsored video by amazon or youtube i just want to give something back to the people that support me the only thing you need to do is to follow me on instagram write down why you think you should win the giveaway on my latest post and if you have any idea of a giveaway item that i could do in the future just write it down as well i don't need any personal data or whatsoever and the giveaway will end on the 8th of february what we have done so far was accepting requests from the forms and sending it immediately back to the database without doing any checks now to give you an example let's go to google chrome let's create a new car now let's add the input fields so the brand name is mercedes the founded is well it should be an integer because we're trying to add the year where a car brand has been found so what we could do is to add a string right here so let's say dory the description is mercedes as well let's submit our form and right here we're getting an error so let's see what it is we're getting a general error incorrect integer value our database is accepting an integer for the input field founded as you can see right here and what we have done was sending back on string to the database so let's fix stuff like this i want to divide the video in two chapters first we're going to cover the controller validation and then we're going to cover the form requests larval has quite a few ways how you could validate incoming data and i want to split that section in two since we have two primary options so let's go back to our code right into the store method that we have and let me actually remove the comments because we don't need it the request object that we have right inside of our controller right here has a validate method that we could use and it will provide a convenient shortcut for the most common validation workflow what we need to do is to add it right above where we create a car so let's say request validate just like creating a new car we need to pass in an array right here so let's add brackets and let's hit enter inside the array we need to pass in the values that we have inside our input field so let's say we have our name pointer and a value now the value right here needs to be required so we're basically tearing our application every single user needs to fill in a name now let's do the same thing for founded which is required as well and for the description we want to set the value to required as well right here you can see five lines of code and if you have worked with php before you might think that this is definitely not enough to do validation well the lines that you see on our screen right now are doing way more validation that you actually think based on what we have entered right here it will determine what kind of error message needs to be printed out so let's rewind let me explain to you step by step what this method does first the validate method will check the incoming data from the request and check whether it is true or not if it is valid so if it's true we will move outside of the request and we will create a new car so let me add it as a comment if it's valid it will proceed so whenever our validation is valid it will create a car but what will it do if it's not vetted well it will throw a validation exception and then it will redirect you to the previous page with all the necessary validation errors next to setting our input fields equal to required we could use a couple other rules now to add them we need to separate them with a pipe so let's go right after required for the name let's add a pipe and what we want to do is to say that our name is unique and it needs to be on the table cars so we will look in the table cars to see if let's say a name already exists and based on that it will send back an error message or it will let you continue to create a car now for the founded let's add a new rule let's say that it has to be an integer another pipe the minimum is 0 pipe the maximum is 20 21 so the year that the car has been found has a minimum year of 0 and a maximum of 2021. now right here we're passing it in as an array but you could also pass it in as a two-dimensional array so what we could do is to add brackets right around required just like this comma and let's do the same thing for unique just like this but this is not something i recommend i prefer to use the pipes so let me undo everything all right now there's a bunch of rules that you could use right here and it will take ages to add them all in one single video i'll recommend you to look at the laravel documentation to see which ones there are i will only show you the necessary ones that i always use and during the next few videos you'll be seeing some more rules that you could add now if we hop to our browser and refresh it our error message is still appearing on the screen that's because we somehow need to print it out in the ui so let's go to our create.play.php right here and let's go to the bottom of our page and let's say right below our form create an if statement and what we want to do is to use the global errors variable and we want to see if there are any errors so if there are errors we want to print out some stuff so first a div with a class of width dash four four slash eight margin is auto and the text excuse me and the text is center so then we need to create it for each loop because we're getting back an array so there could be more than one error message we're going to loop over errors all so all the errors that we have has one single error and what we want to do then is to print them out as a list item let's give our list item a class of text as read as 500 and list none and inside our list item let's print out variable error save it let's go to google chrome refresh the page let's actually undo it add a new card let's say give it a name of mercedes found it in let's say a string description is mercedes as well let's submit it and they found it must be an integer have been printed out but something happened with our ui so let's go to our create blade again well let's copy your entire if statement and let's paste it right below our div save it google chrome refresh the page and let's actually do it one more time mercedes mercedes mercedes submit and the founded must be an integer have been printed out let's add one more car without the name so founded is 1918 description is mercedes submit and the name field is required has been printed out and this is how it works we haven't set the messages right here that will be printed out whenever the request goes wrong now a cool thing laravel provides for us is to create our own custom rules and in order to do that we need to run an artisan command so that's hop to the command in here let's say php artisan make me a new rule called uppercase let's hit enter and our rule has been created successfully so let's go back to visual studio code now to customize our rule we need to go right inside the app folder that we have and then you can see a rules directory right here that has been created so let's open it and you can see the uppercase.php file if we open it you see two methods that are created for us obviously we have the construct but we have the passes and the message now let's start off with the passes now this accepts an attribute name as the first param and the user provided value as the second param what it will do is return a boolean whether or not the input passes the validation rule then it will go to the message method that we have right here which will return the validation error message so let's create it nd passes let's say return string to upper we want to pass in the value and we want to equal equal equals see if it's equal to value now for the message that we have right here let's get rid of everything in here we could say that the column attribute which is the placeholder in your message for the attribute name so let's say the attribute must be uppercase let's save it and this won't immediately work because we need to call it somehow inside our controller so let's go to our cars controller at the top of our page let's require the file so use app forward slash rules forward slash uppercase let's go back to our store method so let's say that we want to tell our application that the name needs to be in uppercase so let's get rid of the rules that we have and let's say that we want to create a new instance of our uppercase that we created so new uppercase without a typo new uppercase alright if we save it let's go to the browser refresh it write down a brand name without a capital founded in 1918 and the description is mercedes submit it the name must be uppercase have been printed out and this is the exact message that we created in our uppercase.php so the f reboot must be uppercase now obviously i'm not really sure if this is the right example but it's just to show you how these little things that make laravel so incredible work this was it for this video where i showed you how to validate forms through the controller if you do like my videos and you want to see more leave this video a thumbs up and if you're new to this channel please hit that subscribe button
Info
Channel: Code With Dary
Views: 3,526
Rating: undefined out of 5
Keywords: laravel, laravel 8, laravel php framework tutorial - full course for beginners 2020, laravel 8 tutorial for beginners, laravel php framework tutorial full course for beginners, learn laravel for beginners, learn laravel step by step, laravel full course, php laravel youtube, laravel tutorial youtube, how to learn laravel, laravel tutorial 2020 - the complete developer course, laravel tutorials from scratch to advanced, laravel validation, validation in laravel 8
Id: o4utwSsFtsU
Channel Id: undefined
Length: 11min 41sec (701 seconds)
Published: Tue Jan 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.