Validation | Laravel Livewire 3 from Scratch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to episode 5 of the tutorial series Live Wire from scratch my name is William Juma the code for each tutorial is in my GitHub repository Link in the description in this episode we are going to look at form data validation in our previous video we looked at among other things the rule attribute in this episode we will look at other ways in which we can be able to validate form inputs and I would like us to begin talking about the rule attribute because we actually used it in the previous tutorial to validate form inputs if you did not have a look at the previous tutorial video you can be able to check video 4. but I would like to take you to the code that we had so inside our register user .php we imported the role attribute and then we went ahead to step the rules and as you can see we are using attributes to just place the rule above whatever it is that you're validating so above the name we are validating the name above the email validating the email above the password we are validating the password I would like to just modify the attributes name and use a custom attribute just as a way of explaining something that we can be able to do as a variation to whatever it is that we had here obviously in our form we have this one called name email and password if we attempt to submit as you can see the first option is saying the name field is required sometimes you find that whatever it is that you have in your form for example you have um fast underscore name so in this case if that is what is displayed in your form or in other words the inputs that you will be having in your form in The Wire model in this case that first underscore name is what will be displayed and in this case that will not look good so for example if what we have here was something like Fast underscore name fast underscore name like that obviously we will need our attributes our property in our in our register user to be having a similar name so probably this will be called Fast underscore name let me just paste if there's something there's nothing so let me just type it in so fast underscore name like that so now if you attempt to submit the validation error that is going to be shown was supposed to be fast underscore name but in this case I think the underscore has been removed and I think that is a good thing but another example would be for date of birth for example you have written it D or B in this case if that is what we are having here the obey let us assume there is a field for the date of birth then also in our register user we have DOB in this case we will have a problem when we attempt to submit now it will tell us dob field is required and in this case it might have some unnecessary challenges in this case so let me just modify and return everything to name and save and also change this one and call it name now if we want to give a variation of whatever it is that we want here where we have the rule we can be able to State what or how we want our the message to be formatted so in this case how our attribute the attribute name should be stated so for example we can say as name as we can say maybe new user name for example now if we attempt to submit without typing in the name we will get a validation error that the new username field is required in one way or another we will be able to have modified our error message it is also possible for us to provide an entirely custom error message that is different from the usual laravel error messages so for example this one you just see email field required so in this case if we if we want to do something custom for the email probably we can be able to pass a message but I would like to still just use the same one that we have here so instead of having as new username I would like to say the error message like that and now I can be able to type inside here the name or in other words you are name is pretty quiet that's I saved that if I attempt now to submit now you can see your name is required so that is how we can be able to customize our error messages but this is our first method of validation which is an extension of what we did in our previous tutorial and it is the rule attribute I want us to look at a different way in which we can be able to validate our our form in other words errors so in this case we can be able to go ahead and uncomment everything regarding the role attribute so I will uncomment this one I mean I will comment it out and comment out that one also and even the password I'm going to comment it out the second method in which we can be able to validate now I will also comment this one out temporarily the second method we can just be able to say validated and then so this one we're just storing whatever it is that we're having inside this variable so instead of even having that we can I can just say this validate so in this case we can say this validate like that and then we can be able to have our array of validation so I can say name Double Arrow required like that and then a comma you can be able to copy this one and do validation for email you can say it's required we can also say unique for the table users and then we can also say it has to be an email like that and lastly we can do a validation for the password and then we can just have it as required and probably a minimum probably a minimum over seven characters for example so once we have it like that we can be able to cross close this with a semicolon also this one will be able to do a validation I would like to attempt and see if it works so if you click there and there you can see it's being still validated I can even refresh and attempt to click on it you can see it's still working so it is very okay for you to validate in this fashion I would like us to look at another method in which we can be able to validate and in this case we can be able to validate bye defining custom rules in other words a rule method in our Live Wire class so allow me to comment this one out so I'm going to comment everything that I have just put it in here right now like that and then of course we can be able to use that other method and I would like to have another function so you we have the function for saving maybe we can have it above here so we are going to call it public function rules like that and inside here we are going to have our rules so basically we have the calipresses and then inside here we can be able to return and you can have our array of values and so what we are going to say again in a similar fashion name required sorry this one has to be in yes like that name required email required and of course I will add something to email password required but you can say minimum there are seven characters for the email we can be able to check that it is unique for the table called users and we can also set it to be a unique I mean to be an email nothing else so we force our users to ensure they have typed in a valid email address so basically we can be able to close that I would like to refresh this and attempt and see if we'll be able to have validation errors we are having one to three meaning there is something that I have not done we after declaring the rules you're supposed to sort of like use the rules in other words use this validate so once we use it then we'll be able to have the validation error so let me refresh again and attempt to submit lovely now you can see our errors are still being displayed I want us to look at the last method in which we can be able to also do custom validation and so for us to use the last method I would like to go ahead and comment this one out such that we won't be using this one so let me just comment it out that fashion I like that so our last method in which we can be able to do validation we will be able to import the validator so in this case our laravel validator facade so here we can say use illuminate facets support facets validator at once we have imported it we can be able to go ahead and use it so for us to be able to work with it we can validate in this format we will actually be able to do our validation inside our save function again so inside here we can be able to say validated somehow just saving our data inside this variable and then we can say Valley data make as you can see this is purely laravel you know and then inside here we can have an array of values but uh we will be having uh each one of them individually so we want first of all to have what we are validating so in this case is um the data that you are validating that uh that we are validating obviously as you can see we have name we have name and the name we can actually say that obviously you know for us to be able to access values the properties that we have set above here we usually use this so this name sort of like to say that is exactly what we are looking at and then email we can have this email and then password we can have sorry these password like that and after that now then we can be able to go ahead and now put down our validation rules so this one is just Declaration of what we have validating so then here we can still have an array of values and you have comma in between here after the first square brackets and inside here now we can be able to put now the validation so we can say validation for name property for the name attribute we can say it's required and then the email we can say that it is required and then it has to be unique for the table users so in other words nobody else should have an email similar to the one that is in existence in the database and then obviously we want it to be a valid email address finally for password we can say that the password is required maybe we can be able to say a minimum of uh maybe seven characters and then finally we can be able now to sort of like format the message in which I mean the message which will be passed to our users so in this case we can just say when we have required the message that should be provided is the in this case attribute so in this case we can say the name or the email or the password and then we can say field is required obviously you can be able to type in whatever in whichever way you want it to be so after that we can chain validate and then now this will be able to do our validation so I would like to refresh again and I have saved the changes that we have made here now if I maybe I can just extend such that you're able to view everything that I have here I've been able to condense it a little bit so so validate I make all that all that all that happened till here I want us to attempt and see if we will get the errors and we will be having an issue because it seems as if it's something that we are still having let me see so eliminate support facets validator make name this name email this email password is password and then name is required email is required and so on and so forth the attribute even that one I think is okay let me save and try again I do not know why we having issues it says missing rules rule property a lifeline so let me check again oh I need to remove this is something that every other thing is okay the challenge is that I have this validate this is the reason why I'm having that error so let me refresh again and attempt to submit now you can see still the validation is a being done and even if I attempt to type something and attempt to submit again again the password you tell me now this seven seven characters if I type something in the email and attempt to submit of course we because we have said it to be required it's saying we need to have an at if I attempt to submit that still we are we are having the password as a problem and the name is also required of course the email is checked because now it seems it is a valid email according to our validation basically that is that there are several things that I have left out I think like two things but in sort of like a validation of arrays that one will be able to do once I have a meaningful example that you can be able to use and then on top of that there is another sort of validation method whereby we use the boot method and that one I will not be able to talk about it in this tutorial episode but uh this means we have reached the end of the tutorial episode please subscribe to my channel hit the notification Bell by doing this you'll be notified when I upload another awesome video if you like the video you can give it a thumbs up you can share it you are also welcome to share to engage in uh the comment section and tell me what you feel
Info
Channel: LaraPhant
Views: 823
Rating: undefined out of 5
Keywords: livewire components, livewire 3, laravel, livewire forms, validation, livewire 3 validation
Id: dM4h123jg14
Channel Id: undefined
Length: 21min 13sec (1273 seconds)
Published: Wed Sep 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.