This episode is for members only

Level up with a premium membership

Join now
Already a member? Sign in to continue
Playing
41. Validating the registration form

Episodes

0%
Your progress
  • Total: 4h 32m
  • Played: 0m
  • Remaining: 4h 32m
Join or sign in to track your progress

Transcript

00:00
Let's take this episode to validate the registration flow.
00:04
And in the next episode, we're going to be covering creating a custom validation rule to check if the email address that the user provides when they register is already in the database. So in this episode, we're pretty much just going to be doing everything that we've already done. So feel free to do this on your own or skip it. OK, so let's head over to our
00:25
register controller and let's just start to validate as we did before. So we'll actually borrow the namespace from here just so we don't have to keep doing that. And let's go ahead and pop that at the top just here. OK, so when we go ahead and send this through, we already know that we need a try catch block. So let's go and implement that first. Let's pull in the validator
00:48
exception. And we know that we're going to flash a message in here once we have our rules. OK, so what rules do we want here? Well, we've got a key here of the user's name. So again, you can choose which rules you want, but I'm going to go ahead and say that this needs to be alpha and it doesn't want to be empty. Now, I'd also recommend I'm not going to do this now setting minimum or
01:10
maximum values on any of these fields just so the user doesn't enter too much data. OK, so the next thing is going to be the email address. Once again, we know that that needs to be an email. So let's use the email rule. And again, we'll say not empty. And last but not least is the password. So let's add one for this. And we're just going to say that this is not empty. But of course, feel free to add
01:35
any validation rules here that you need. OK, so let's bring our session into this controller. So let's pull out our session and let's flash a message if this doesn't quite work. So we'll say this session, get flashback, add, and we'll just use the same key. So errors. And of course, grab the messages from that exception. And then we want to redirect the user back to the register page if
02:05
this happens. So let's redirect them back. OK, so again, we can steal the code from our login page here. So let's just grab one of these and let's head over to our register twig template and add each of the errors in here. So we've got the first name here. So let's change these two over and we have our email address. So let's go ahead and change these to email. And lastly, the password,
02:28
which is exactly the same. OK, so now if we come over and hit register and we see you have not passed a password. So that doesn't look like it's working. Let's go over to our register controller and see why. And yet we forgot to actually assert this. So that's really important. So we get that exception through. And again, we pull that from request and get pass body. OK, we should be good.
02:50
Let's hit register. And yet nothing's happening because we're not passing this through to our controller. Like I said, we're going to be tidying this up a little bit later. But to get this to work, let's pull out all of the errors from our session. So session, get flashback, get errors, and we'll grab the first item in there. Otherwise we will pass null through. OK, we should just be
03:14
about done. Let's just refresh this and hit register. And there we go. OK, so it looks like the first name is not working. So let's go over to our register controller and just try this out. We call that name, of course. So let's just change that over and we should be good. OK, so we are now validating our registration form. But of course, if I try and register a user here that has the
03:39
same email address, what's going to happen? Well, let's hit register and see. You can see that we just get a pure SQL error back because, of course, that email field in the database is set to unique. So rather than get to the point where we show a user an error, we want to validate that this doesn't already exist in the database. Let's look at how we create custom validation rules next.
50 episodes4 hrs 32 mins

Course overview

Starting completely from scratch, build a modern PHP framework with all the features you’d expect.

Whether you’re new to PHP or not, this is a great exercise for learning what happens under the hood, arming you with knowledge you can apply anywhere you use PHP.

We’ll cover routing, controllers, views, the container, accessing the database, models, authentication, config, CSRF protection, exception handling, pagination, validation, flashing messages and much more.

Let’s dive in and build a PHP framework, step-by-step!

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.