 Hey everybody, it's Brian. Welcome to the 11th Ye2 tutorial. We're going to be finishing up on models and if you've kind of gone over the Ye Framework 2.0 API, you know there's a lot to models, a lot more than we've actually covered and probably a lot more than we will cover, but we're going to get into some of the more advanced stuff in this tutorial. So what we're going to be covering is how to make your own custom model and how to do validation. And I would encourage you to go out and read the definitive guide to Ye2.0 while I said at the beginning of this tutorial series it's not even remotely finished but it does have some really good information especially when it comes to models. Like we also said that models are kind of the core of any model view controller framework so we're going to dive right in here. Now two things should become very apparent. There's two different types of models like if we crack open accounts, something that we've generated it extends or inherits the EDB active record meaning this thing goes back to a database but this login form which was pre-generated for us you can see it just extends model meaning it does not go back to a database. So when we look at this login form you see there's all these rules and you know different functions and stuff that you can put in models so that's really what we're going to be covering today. We're going to actually build our own. So we're just going to go model new, PHP file we're going to call this join form and we're just going to make like a generic form so that if someone was going to join our website this is what we'd use. Namespace app slash models and we're going to use Ye and I could probably just copy and paste a lot of this from the other file but I just really want to do it this way because why not? Ye base and we're doing model. Alright so I'm going to make our class here so we're just going to extend the model here and we really don't need that Ye base right here. I just say extends model so that in a nutshell is how you make a basic model you just literally extend model and you obviously you know inherit everything that the model class has but now we want to add some properties here so we're going to say public, let's say email, public, password, whoopsie, repert that's not even a word repert. Alright so if we compare this to our accounts which is you know obviously an active record you see how they've got them up here this was auto generated based off the database table so what you know they're doing is they're making these variables similar to just you know like automatically generating them based off the database and then holding them in memory we're just actually creating them right here and this is actually this property flag this tells it that this is you know something that the class has we don't need to do that because we're building it right into the class. Alright so we've got email we have password and we have repeat and this is going to be like a basic joint form where you'll have literally email enter your password then repeat your password and we're going to bake all that logic right into the model so that you don't have to write any code really beyond this model so we're going to say public function rules and here we're going to have the actual rules of the model itself and this is where it gets a little confusing but just bear with me here we're going to say return of course we're returning an array so the first thing we need to do is we need to tell it what fields are required well we want email pretty much all our fields are going to be required password and repeat so we've got our array now we need to give it a validator as it's called whoops yeah there we go so what this is really saying is we have an array and the array are just these properties up here and each one of those is going to be required because that's the rule that we're putting on there now we're going to make another one and we're going to say that the password is a string meaning it has to be a string can't be anything other than a string and its property min is going to be five meaning it has to be at least five characters long and if you're wondering where am I pulling these magic you know properties from well you can go out to the e documentation you can read about validating input and the reason why we would do this is anytime a user enters input into our website we need to validate that if you don't you leave yourself open to all sorts of really nasty attacks such as you know sequel injection or buffer overflows things like that that's actually how a lot of websites really get infected is they just do no user input validation so he's going to take care of all that for us under the hood we don't have to write any real logic to do it it just knows what we wanted to do because we're giving it these properties then we want to do some magic here with emails we're going to say email we're going to give it a filter this is a new term for you um filter basically says this is what we're going to do to the information and we're going to say the filter is trim meaning it's going to automatically take the email and trim it now what trimming does is it gets rid of any white space so if they say you know space email at blah blah blah dot com space space space it's going to trim out all that white space so you don't have to do that at all it does it automatically now this one's actually pretty killer i like this is one of my favorite we're going to say email and we're going to give another one we're going to call it unique meaning this has to be unique in a specific and we're going to say target class if i can spell target class now what we're doing here is we're just giving it a uh an actual path to a model here if i can spell models counts and if you're wondering if i have all this memorized no i don't i actually have notes that i'm kind of looking at as we go there's no way you can memorize all this well if you do memorize all this maybe you need a hobby like you know go outside do something with your dog maybe you know mobile on meet the neighbors i'm sure they're nice people which ironically i really don't even know my neighbors so i shouldn't say that all right so and we're going to explain all this in a minute now if that looks long it's because it is long and it actually you know goes around to the next line here so basically what we're saying is the email attribute has to be unique the target class is app models accounts so we're saying this accounts class you know our database record basically and the target attribute has to be email now if we look at the accounts class in our database you notice that one of the properties in the database is email so we're basically saying um select star from accounts where email equals whatever the user just entered um and if it exists then we're going to pop back a message saying hey this already exists i think my girlfriend's arguing with a cat in the background i can hear her don't know what she's doing all right so now we're going to work with the repeat attribute because remember we've got email password repeat so we've already done password up here actually let me pull it down here that way it's a little more a little more streamlined and we can tell what's going on here so we're saying our email password repeat are required everything's required then we're going to filter the email using trim and then we're gonna use a validator called unique which is going to verify that that has not been used before the password is a string minimum of five now repeat this is a little bit different we want to do a comparison so we're going to say compare and we're going to say compare compare attribute to you guessed it password so what we're really doing here is we're saying hey this thing has to equal our password if it doesn't then we're going to say give it a message i'm going to say passwords must match all right so there's a lot there i'm going to kind of scroll over here so you can really see this so we're going to say repeat is going to compare to password and if it does not compare then it's just going to say the message is password i'm going to say passwords must match now if you think about just the logistics of what we're doing here we're actually telling you to write all this code for us and we're not really writing any code we're just handing in an array but all of this is being done for us so we don't have to mess with any of it it's pretty slick actually then we're going to do public function we're going to say attribute labels we don't want to include the parent attribute labels let's get rid of that and we're going to return of course an array almost everything in use an array so get used to it and we're going to say email and i just like doing this just because um in case i want to come back later and change you know like if the if i'm doing like a website for a client they go i really don't i don't want to say email i want it to say you know into your logistical address or some nonsense i can just do it right here and this is you know these are the attribute labels so when we build a form it's going to pick these up and use these as the label instead of the actual field names interesting huh all right so let's go into our site controller here and we're going to make a new action make sure i don't have any squirrely logic in here it's going to really mess me up because i know we played with this a little bit here or it's supposed to okay good so we're going to an action join and we are going to return this render join and before we forget let's go down into views actually let's not let's do this instead let's go into here and do i have g up yes in g you remember how to get to g you just type g i i in your route you have a bunch of things we want them to form generator here and we're going to just say site slash join and models equals join form preview this bad boy and you can actually see the code that it's going to generate here and this will very quickly generate that form for us so we don't have to really write any of this code it's just going to say whoop there you go so i'm going to actually make sure that's select create we're going to generate that go back out to our website make sure that uh site actually made join and there's the form that we just generated here all right so now we want to actually pass the model to it and this is kind of a little tricky here you see how in our form that we generated it has this model variable and it's got these other variables got a form it's got to this well model is actually app models join form so what we need to do is actually well create that and then pass it to it so we're going to go into our site controller here and we're going to say model equal new join form and notice how it puts the full path because we haven't included it in the uses up top here and now we need to pass this model to the actual view so we're going to say give it an array remember everything is an array model let me move my mouse so you can see what's going on here so really all we're doing is we're saying this render so we're going to render that view the join view which is the form that we auto generated via g which i'm not sure if this is going to work without any modifications we're going to find out the hard way and we're going to take this model and pass it as a variable called model we could call this you know fuzzy bunny slippers if we wanted to but you know i like keeping things you know pretty simple so model is going to be named model and just as a test let's go out to our site so let's say and we want join all right so there is our model that has been displayed via the form and if we just types of random gibberish in here you can see how it validates some things and not others so you can see the password cannot be blank etc etc so if we do test let me look back at our logic here there's a reason why this isn't working see it should actually validate that it's not an email address so let's jump back in here i think i missed something i had what we professionally call a boo boo yes we have a boo boo we forgot to put something in here we're going to say email and we're going to put a validator on here that email has to be an email and there's some fancier stuff that you can do like you can actually make it go out and talk to the email server to validate that it actually is a valid web server and all this fun stuff but it's not really tried and true especially when you get into like smtp forwarding and things like that it gets kind of messed up so we're going to refresh our page here and then we're gonna see email is not a valid email address think of the logistics of that we just wrote a bunch of code in the background to determine whether or not this is like test at void realms.com see if we do this it'll say hey oops now it'll say hey it is a valid email address but if I take that .com out you know because a lot of people just like to put an at symbol in there not valid and then passwords you know password cannot be blank repeat cannot be blank if we do this and we do that that's how they're different it'll say passwords must match so you know there's all sorts of interesting goodness going on here and we're going to go back here and we're going to play around massage this form a little bit here because I'm not overly thrilled with it just yet well first thing first here let's actually do this we're going to say form field password input so we're going to actually mask that password so they can't see it being typed and let's see if there's anything else I really want yeah you know what let's do this we're going to go open login .php and you notice how in the login form it actually has all this right here what this is is it actually makes a templated parameter and we're going to flip back to join see how it just says begin we're going to copy and paste that array right in there and what this will do is it actually says you know field config template and then it has this div template that we're using so when we flip back here you see how it's like taking up the entire page well if we just refresh it now it does this so just be aware that that exists and that's kind of how you manage that you can pretty much put just about any sort of template you want in there you just have to be mindful of this you know bracket label in bracket and then this bracket input in bracket and then the error right here but you can put pretty much anything you want here so you can make you know one one div red one float over the other move around the screen and all sorts of crazy nonsense now when we go back here you see how it says repeat password and that wraps well let's say this is for a customer and the customer goes well I don't like the way that wraps just make it say repeat or actually let's make it say try again so we're going to go back out to our model here our join form and we're just going to say what did we say try again which is kind of misleading but we'll enter try again and then when they update ta-da it says try again and we have all that logic in there and it won't submit until all of these are done meaning they all are green meaning they validated so one thing that this model will do because it's a model is it will validate it will not however save meaning you cannot go out here and say model save see how you know even intelligence is going to tell you hey buddy that ain't going to work let's actually try it just to see I've actually never tried this let's just do it see what's going to happen here I gotta dollar says yeah Lee's just going to explode see this is like when you give your cat way too much catnap it just says nope not happening so and that's because it's not an active form it's just a model so what we want to do here is we want to well first off make sure that this thing validates so I say if my girlfriend bought me this this chair for valentine's day it's like most comfortable thing ever it's like my butt is so happy right now all right so we're going to load this bad boy so what are we going to load what we're going to load the the request basically so we're going to say e app requests and we're going to load the post data out of the request it's not a function what am I thinking so if we can load that post and we could even go a step further here and put all this on one but you know what we'll just break it out just so it's obvious what's going on here I say save here meaning this is where we would actually do something with that data right otherwise we're going to say ye warning maybe warning really I'll probably help if I did the other colon all right so basically this is like a template so if you were actually doing this what you would do here where it says save here and I'm just going to comment this out because we don't want to actually do this but you would say like account equal new accounts and then you would say account email equal model email put that and then you know same thing with password etc etc whatever field you wanted and then you would say you know account validate and this would obviously be within an if then statement and then account save and that's hopefully going to answer some emails that I'll get of well how would I actually save that data that's how you would really do that you would make a new instance of your active form model the account where'd you go or accounts sorry you'd make a new instance of this and then you would populate it from the other model so you'd say email password and then you would do whatever you wanted here and then you would validate and save this down to the database so let's see that and we're just going to show this in action um if it doesn't validate we will you know see the issues with the model if it does validate we'll see save here in the log um and typically this is also where you would do like a uh this whoops hand listen actually it's a return this dot redirect and don't worry we'll be going over this sort of stuff in future tutorials I just wanted to kind of you know show you because I know whenever I do one of these everybody goes why how do I do this how do I do that well that's you know kind of the basics of it here but really I just wanted to focus on how to build an you know a model from scratch and I control it so we're gonna make sure this goes back the way it's supposed to I'm gonna say see it won't even submit right so we're gonna say and test it voidrums is not an actual email address so if you email that it's probably gonna bounce back we're just gonna say make sure they match there we go and we'll check our log and see save here now in real life you're probably never gonna see this you know issues with the model because the validation is happening before you even do this post back but it's always good practice to do that just in case somebody tries to slip something in um you know they try to do a sequel injection attack or something like that it'll not validate correctly most of the time I say most of the time because there's always ways around things so that's all for this tutorial I hope you found this kind of educational entertaining and let's see here you can read more about creating forms in the definitive guidey 2.0 which is out on yeast website and for other tutorials also visit my website voidrelbs.com this is run 100% off your donations so if you're feeling kind of rich feel free to donate otherwise be sure to join the voidrumbs facebook group we just breached over 400 plus developers in there so that's getting kind of crazy we got a lot of good conversations going on there all different languages like you know php with yee c++ python I think there's a couple of pro folks in there some embedded folks it's kind of crazy all right that's it