Codeigniter App Tutorial
Lesson 5 of 10 · Codeigniter App Part 5 – Form Validation · IT & Software
Codeigniter App Part 5 – Form Validation
alright so in this section what we're going to do is we're going to create our registration form and we're going to implement the form validation class or a
library that comes with code ignitor and if you go to the documentation and click on form validation there's a ton of information here but if we just look at the syntax the first thing that I want
to show you is this all right so this goes in the view and what it does is it just echoes out the errors that you get if you set a field to four acquired and it's empty when you submit it this is
where the validation error is going to go if you have multiple errors than multiple errors will show all right so that's that now to implement it in the controller you can see in this example
it's loading the form validation library and it's loading the form and the URL helper and we don't have to do this because we included all of these in our auto load file what we do need to do is
this here all right so we're saying if this form validation is false this means if the form hasn't been submitted all right that's what we want to do and all we're doing here is calling the view if
the form is not submitted if it is then this particular example is just loading a success page all right our actual code is going to have some database code into it we're
going to shoot off the model and upload or insert our registration info all right so let's go ahead and let's go and we need to create a new view so under views users let's create a new
file and save that as register dot PHP all right and what I'm gonna do here is just paste in the form because we've already gone over the form helper so it's kind of redundant to just to type
all this out so let me paste this in here I'll go over real quick we have a heading a paragraph we have our validation errors okay this is where the errors will show for our registration
one thing I do want to do is stick in the bootstrap class for an alert all right so we'll get the not nicely formatted bootstrap alert so let's go ahead and replace this with the
bootstrap class all right so next we have our form open we've gone over this we're submitting this form to the users controller and the register method which will create we have a first name we have
last name email address user name password confirm password and then a registered button alright so that's all we need here so let's save that and let's go to our controller
and we need to create our users controller users dot php' and this is also where our login method will go when we get to it the reason that I didn't complete the
login yet and I'm moving on to the register form is because the login form wasn't a good example to show you the form validation class this is much more suited to show you how that works
alright so for the class we're just going to say PHP class users extends CI controller all right so let's put in a public function public function register all right so
this is where we're going to submit our form to and what we want to do here is we don't want to just throw in our view because we need to check and see if the form has been submitted or not you could
use two different methods you could use a farm method to display the login form and then have it submit to another method to process it but I like to keep it all in one
it just seems ideal so we're gonna set some rules here all right so we want to say this form validation and this form validation set rules alright so in here is where we want to
put our rules you have a couple different parameters here the first is going to be the the name of the field in this case is going to be first name the second argument is going to be a
user-friendly version of the name so I will say first name and then the last will be the rules that we want to have so we want to trim this and look any quotes what trim will do is take off any
white space before and after the field all right now to separate these rules we use this pipe character alright so the next thing we want is required ok they have to put in their first name although
they'll get an error next we want to put in a max length all right and the max length will go inside of brackets so for the name we'll say 30 cap now let's we'll say 50 characters all right and we
can also do a min length and for this we'll put too all right so the last thing I want to put on here is XSS clean and that just protects us from any XSS database
attacks or whatnot all right so I'm going to copy this whole line and we'll have the same rules for the last name as well alright so next would be the email all
right so let's just paste this in and the name of the field is email and where I'm getting this name is from the actual name value okay so email let's see email we'll change
this to a hundred for the max length minimum length I guess would be five right yeah okay and XS is clean we'll keep that there's one more rule I want to add here and that is valid email and
that will make sure that it's a valid email address very easy to use this this helper than to try to do this by yourself you know we put this in and it automatically checks the email for us so
we don't have to create our own function to do that all right let's see got a couple more this will be username user name ah let's see
all right we'll keep the same let's see the username will have a minimum length of six and let's see the max length will say 20 all right now what I want to do for our username and email is create a
custom rule all right I want to create a custom rule that's gonna check the database to see if there is already that email or username but I'm not going to do that right this second but ultimately
we're gonna want to do that we don't want two of the same usernames or emails so we need some way to protect that so the next one will be password and for password
let's see I guess we'll make the minimum length six as well same as the username and then we have a confirmed password which let's see what's the name of that
password to and that's another thing we need to check to make sure that both they both match which is pretty easy to do so that's password two
and for this we'll just say confirm password alright now what we want to do here is we want to use the matches role alright so say matches matches and then we just want to put in the name of the
field that we want to match with it which is now sorted all right so that that's also something that would be kind of difficult to code not difficult but just time-consuming so so it's great
about using a framework like this all right so that's all of our rules the next thing we want to do is to check if the form has been submitted or not and we can do this with an if statement this
is going to be an if-else and what we're going to say is if this form validation run alright so yeah actually we're gonna do the front equals false so if it
didn't run we're gonna do something and what we're gonna do is we're going to load the register view so we're gonna use the same format as we did here using a view in basically a template or a
layout all right except this time our main content is going to be in the users folder and it's gonna be register view all right so
let's see what we got if we go to users slash register all right so there's our registration form I actually want to put a link to the registration form up here so we'll
do that in the layout so if we go to views layouts main and right here and this is a good time to show you the URL helper all right so we auto loaded this I'm pretty sure and there's a few things
we can use this site URL which will give us our site URL segments which are for instance this URL here this is the first segment this is the second segment so we can we can refer to these in our code
using the URL helper all I really want to do is display hey I just want to add a link so we can do that with echo base URL well actually yeah we've been using this all along actually
we used it right here so we'll just put that in that goal base URL and we're going to go to users slash register that's gonna be the link so he actually have to put the link
all right let's make sure that's working so who go home actually home is not going anywhere so let's fix that all right so home we'll just grab this
here biess URL that should do it let's also add this to the logo as well well all right so that goes home all right
think we can click register takes us to the forum all right so what we need to do now actually let's try this let's just leave them all blank to see what happens all right so you see that we
have all these errors actually don't know why that's in the check let's check the register view all right so we have an extra quote and greater-than so get rid of that all
right so there's all our errors okay so they include the fields that are required the characters in length the confirmed password is required and if we do fill out the confirm password you can
see that we get the confirm password field does not match the password field all right so pretty cool so that's our validation for the form which looks good now we need to we need to insert this
into the database and to do that we need to use a model and I don't want to go over models in this in this video so in the next video will actually submit to a model which will put our user into the
database
We will use the form validation class with our registration form for our TodoApp
The full source code is at – https://github.com/bradtraversy/mytodo_ci