Diagnosing missing old input0:00
Alright, so let's see. We've been making pretty good progress on our refactor, so now we can return to something that you might have missed. Notice if I try to log in with phony credentials here, yes, it redirects back, yes, we see the validation errors, but notice I don't see the old form data. Alright, well, you'll remember a number of episodes ago, we actually solved this. So, for example, if I go to session/create.vue, yeah, like many episodes ago, we were still relying on the old system where in response to the POST request, we returned HTML. So that means, or that meant, we could then do something like this, where we say, alright, well, if there's any email in the POST request, then display it, otherwise default to an empty string. But yeah, of course, now, well, that's not going to work because we're using a new system, and I've already forgotten the acronym again, post redirect
Manually reading flash data2:18
And then we'll clean it up. Alright, so look in _flash, look in old, then look for email and use that value. Otherwise, again, default to an empty string. Alright, and I'm sorry, let's close that out. Alright, let's give it a shot. So we'll come back and give it a refresh, try to sign in with phony credentials. And notice I provided an email and a password. But if it redirects back, we only repopulate the email field, which again, is which again is expected behavior. Okay, cool. But yeah, I don't I don't like this at all. And let's just see if we give it a refresh. And yeah, that's fine as well. We don't end up with any errors due to the way we structured this. But yeah, it's just cumbersome to write. So let's go back into our Session class. And yeah, we do have that get method that will automatically look for the flash key. Alright, so let's come back and switch over to that.
Using Session getOld3:08
let's go back into our Session class. And yeah, we do have that get method that will automatically look for the flash key. Alright, so let's come back and switch over to that see if it's any better. We have to use the full class path, which is Core\Session::getOld and then look for email. Otherwise, if that's no default to an empty string, right a little bit better. Let's come back, give it another try. Yeah, that seems to be fine. It repopulates the email. I give it a refresh. Yeah, this is expected behavior. But again, it's just a little bit cumbersome. I hate having to provide the full class path. Yeah, it's not perfect. Why don't we tweak this? Let's grab all of this. And I'm going to switch over to my functions.php file and add a new helper function. How about something like well, again, it's a common convention to use this function name called old. For example, once you graduate
Creating an old() helper3:58
over to my functions file and add a new helper function. How about something like well, again, it's a common convention to use this function name called old. For example, once you graduate to something like Laravel, Laravel will have an old function that gives you old form data. So that's why we're sticking with that name. We're going to give it a key. And yeah, if I were to paste all of this in, it would be nice if I could just do something like this. But notice here we are assuming if you couldn't find old form data for this key, just return an empty string. Maybe instead, you can override that if you want. So we will set a default to an empty array. But yeah, if you want to, when you call this function, you can override that default. And I'll give you an example of that in a minute. Okay, so now we're just taking a slightly cumbersome line and wrapping it within a less cumbersome function. Okay,
Testing helper and defaults4:46
that default. And I'll give you an example of that in a minute. Okay, so now we're just taking a slightly cumbersome line and wrapping it within a less cumbersome function. Okay, so now I should be able to say oldEmail. And if I come back, let's give it a shot. We cross our fingers. And yeah, this is what I want. So now if I give it a refresh, that works. And yeah, of course, if I could override the default, we wouldn't want to do that here. But just to show you how this is working, you can set a custom default value if and when it makes sense. So for example, sometimes what you'll want to do is if you have like an existing User, you could do something like this. We'll get the oldEmailAddress from the formRequest. And if you couldn't find one, default to the current user's emailAddress. And yeah, now we have the ability to do that. But yeah, I think this is precisely
