This episode is for members only

Level up with a premium membership

Join now
Already a member? Sign in to continue
Playing
45. Persisting old form data

Episodes

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

Transcript

00:00
Another really useful way that we can use middleware is to flash old data within our forms.
00:05
And why is that? Well, let's go ahead and enter an email address to sign in and then hit login. You can see that the form field here value disappears. Now, that's not too bad for something like a login form.
00:17
But when we're registering an account, particularly if we have lots of fields, we're going to end up losing all of that data. So let's see how we can use middleware combined with our session to flash this old data to show it in the form if there are any validation errors.
00:31
OK, so we're going to head back over to our middleware section and let's create out a new piece of middleware called flash old data. And we'll end up with middleware. OK, so let's go ahead and implement our middleware interface again.
00:46
And let's go ahead and pull in our process method just in here. OK, great. Right. So what we need to do, because we've got a request here,
00:56
we basically want to grab our container, add all of the request data that we have within that form to our session flash. Then we want to build out a tweak function to pick this up from the session. Let's try this out first of all.
01:12
And then we'll see that we'll need to slightly change this middleware. OK, so we're going to go ahead and grab out our container. Let's grab the instance of our container. Let's go ahead and get out our session functionalities so we can flash.
01:25
We've already seen doing that already. And we're going to go and set. We're not going to use add because we just want to set one individual key. We're just going to set this and call this old.
01:35
Now, we already know that from our middleware, we can just get the past body of any request. You can test this out by copying and pasting and dumping this around. OK, let's go ahead and return using our handler.
01:47
And we will handle the next piece of middleware or the next request, passing our request through. OK, so now every time we submit a form, the data is going to be flashed to this session key called old.
01:57
Let's build a tweak function to try and grab this out. Now, before we do anything, we want to make sure we add this to our global middleware. So let's go and pull in new flash old data middleware. And that will now be applied to every route.
02:12
As a side note, you'll probably only want to do this when the request type isn't get, because when we get a route, there's not going to be a past body. But we'll leave this as it is for now, maybe come back to it later. OK, so now that we've got this in our middleware,
02:26
it's just going to work in exactly the same way. But we're flashing all of that data now. So if we head over to our twig runtime extension, let's go ahead and pull out a new function in here,
02:36
a new method in here called old. And let's go and grab directly the session that we already have defined in here, just so we don't have to repeat ourselves. So from here, we want to get the flashback.
02:48
And let's say that we want to get that old data. And then we want to access the key that we want in here. So we need to pass through a key to be able to access this old data. And we'll see how this will look in a minute.
03:01
So otherwise, we'll just return null. OK, let's register this function. And then let's see how this is going to look in our templates. So let's pull this in here.
03:09
And we'll just call this old. OK, so over in, let's start with the login template. Let's just do this for the email address. It's not recommended to do this for passwords.
03:19
So let's go ahead and use that old function and grab out the old email address that's already been flashed and then retrieved using this function. OK, so if I go ahead and enter an email address and not a password,
03:29
it looks like that isn't working. Let's just double check what we're doing over in our middleware here. And yeah, so we are getting the session. We're setting this, but we're not doing it within our flashback.
03:39
So let's grab our flashback first and set that. That's the first mistake. OK, if we come over and try this again, you can see it still does not work.
03:48
Now, the reason it doesn't work is because what we're doing here is doing this before we hit the next request. What's going to happen is if we flash this here and then we handle the next request,
04:00
by the time we get down to our route and we show the page, this flash data is going to be lost. So we want to switch this up to after middleware. So after we have posted the form, then we want to flash this.
04:13
So we already know how to do this. We can just create out a response in here, grab the handler and assign that up here, then do this afterwards and then return the response.
04:23
So let's change this up to return the response down here. And we should be good. Let's try this out one more time. And there we go.
04:30
Our data is kept in here. But of course, because we're flashing this, if we refresh the page, as we would expect, this data disappears. OK, now that we've done this,
04:39
let's just head over to our register page and do this here as well for the first name and the email, and we should be done. So let's grab the old first name, if that exists. And let's do the same here for the email and grab that out as well.
04:53
OK, let's go over to our register page and try this out. So I'm going to register a new user in here, hit register, and we come across another problem. So the reason that this happens is because when we get something
05:07
out of our flashback, it technically unsets it. So let's just examine this. So I'm going to go ahead and open up our flashback class, which is returned to us when we use that flashback.
05:19
And if we look at the get method here, what this does is it will unset it after we access it. We know where we're accessing it. We're doing that within our twig function.
05:28
So over in our twig runtime extension here, we're getting this old data. And at this point, when we get the first item, which is the first name, the next time around we try and access this, this data doesn't exist. So any fields after the first one that we access just won't work.
05:46
Now, luckily for us, if we head back over to our flashback, we have a peak method, which allows us to return this, but not unset it. So let's go ahead and do that first and see what happens over here. OK, so back over here, let's go ahead and change this over to peak.
06:02
And just so we know that we're working with the correct data and everything is coming through, let's dump this and kill this in here. So I'm going to go ahead and fill in the email again, hit register. And yeah, we're still getting the same data back, but this won't be lost now.
06:16
So let's change that method over to peak and we should be good. Let's go over to register and we can see that it's hanging around, which is absolutely fine. We can change this up, but now at least we're getting both pieces of data.
06:29
Now, we don't want this data to necessarily hang around. So let's just see how this works. I'm going to hit register. That's perfect.
06:35
When I refresh the page, that's absolutely fine. That was just because we went back a page and that data still existed. So we've peaked into this data now. It has been unset now.
06:44
And now we're getting this old data as we expect. Same with the login page. It's still going to work. If we refresh the page, it's lost again, which is good.
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.