Tip:
Highlight text to annotate it
X
So now we have a functioning add form to be able to create content in our database. But
before we release this for other people to use, we need to make some modifications here
to make sure that the data is what we expect before we actually input it into the database.
So in this next step, what we'll do is check to make sure that the information that's being
supplied by the user fits certain parameters. In some cases, we'll check to make sure that
the user has supplied any information. For example, we definitely want the user to supply
their username, password and first name.
Next, what we'll do is check to make sure that the user is inputting a number for both
the birth year and the shoe size. And then we'll check to make sure that the username
here is unique, that it doesn't exist in the database yet because part of the point of
a username is that it's unique for each user.
So let's jump to the resource pack directory and open up the eighth step here which is
called crud-form-validation. And we'll copy this and paste it over the code that's in
the test dot php file and we'll save it. Now, let's go ahead and do a demo of this form
inputting some information that's not quite right so we can see what kind of errors we
get. So we'll skip putting anything for the first name or the birth year let's put some
characters in there that are definitely not a number. We'll do the same thing for shoe
size. For username, let's type in admin which is an existing user already, and we'll leave
the password input empty as well and then click add entry.
Okay, so we got some errors here. It says "Please enter a value for first. Please enter
a value for password. Enter a number for birth year and shoe size." And then it says, "Sorry,
it looks like that username is already in use."
Now, there's three major reasons to do validation. The first is security. We want to make sure
that the user can't input something into this form that's going to be pulled into the database
that we definitely don't want. Second, as a courtesy to our user, we want to make sure
to let them know when they've input something that doesn't seem quite right so they may
accidentally type in certain information in the wrong input we want to let them know about
that. And third, we want to make sure that we have the right information too. So it's
a courtesy to the user, but we also want to make sure that our data is accurate as well.
And so validation helps with that.
Now, before we step through the code, let's do a file comparison between our last example
and this example where we've added in validation so we can see exactly where the changes have
been made. So I'm going to go back to the resource pack directory, and I'm going to
drag the seventh step over to my file comparison program and put it on the left and then I'm
going to take this eighth step and drag it over to the right-hand side and click compare.