Transcript
-
So as we saw whenever we were inspecting our flash messages, it actually has the old values that we submitted our form with originally
-
on the flash messages itself available to us. So we can use that information to actually pre-populate the information back after we've performed validation so that we don't have to sit here
-
and retype that information over again, attempting to use valid data. So let's hide our browser back away, jump back into our register page, scroll back up, and let's start with our full name.
-
What we wanna do is provide a value into the input. We'll do double curly braces so that we can reach into our flash messages and we'll get the field name, which would be our full name.
-
Now, if we save here, jump back into our browser, you'll see that we're gonna get undefined. And that's because the flash message is returned back undefined because it doesn't have a value presently. So let's hide that back away.
-
Then we can do or an empty string there. Okay, we'll give that a save, jump back into our browser, and now that undefined is gone and our value is now just an empty string.
-
Let's type out John. Let's retest with invalid data so that we can test this flow and hit submit. So there we go. So we see that our John full name is still provided and we haven't applied this yet to our email.
-
And for security reasons, it's going to exclude password information. So even if we were to add it to our password, it would not be populated. And just like everything else within this validation flow,
-
AdonisJS has provided helpers to us to make this even more streamlined. So let's hide our browser back away and we can replace this flash messages get call with just a method called old and provide in a key for the old value
-
that we want to reach for. And this will essentially do the exact same thing, just in a more streamlined method. So let's go ahead and scroll down to our email and apply it there as well.
-
So we get a value old email or an empty string if it's not found. And just to show you that our password is excluded from this, we'll go ahead and add it here as well.
-
So old password or an empty string. Okay, let's jump back into our browser and give it one more failed test.
-
So John test@test.com and some invalid password. We'll hit enter. And there we can see we still have John, we still have our email,
-
but our password there was excluded. And so that's how we can reuse values that were submitted with the form originally, despite the validation failing.
Reusing Old Form Values After A Validation Error
We'll learn how we can easily repopulate our form input values with their previously submitted values when our VineJS validator finds and sends back an error.

- Created by
- @tomgobich
- Published
Join the Discussion 0 comments
Be the first to comment!