This episode is for members only

Sign up to access "Learn Inertia" right now.

Get started
Already a member? Sign in to continue
Playing
09. Resetting forms

Transcript

00:00
From the last episode, we saw that the text that we'd enter into the text area
00:04
hangs around even after this has been submitted. Now, at this point, that should be pretty obvious. It's because we're not doing a full page reload. Instead, we're just re-rendering this component and the data within it,
00:15
which means that we see these comments, but anything like form state tends to just hang around. So what we're now going to do is look at probably the most used option that you're going to pass through when you are submitting forms.
00:28
Now, we pass these options through to the second argument of whatever we're doing with a form. And there's a ton of things that you can do in here, which we'll be exploring later.
00:38
But most of the time you're going to be using on success. So this is like a callback to when this form was successfully posted. And you can do anything in here. It doesn't matter what you put in here.
00:51
You might want to clear stuff up. You might want to flash a message, which we'll be looking at later as well. But for now, what we want to do is just clear the form out, reset the form. Well, let's take a look at how we might think we'd originally do this.
01:03
We know that form body is accessible as a property on here. So let's try this out first, form body. And let's just send this back to an empty string. This is probably what you would do in a normal view application
01:15
without a form helper like this, if you were working with some sort of ref or reactive object. So let's just write something in here, hit post. And you can see, sure enough, that does work.
01:26
But this is a little bit cumbersome if you have more data in your form. What you really want to do is lean on some of the helpers that Inertia gives you within its form helper. And one of them is reset,
01:38
which is basically just going to reset this back to the original data you included inside of the use form object. So let's take a look at this in action. I'm just going to say hello again, hit post.
01:50
That gets reset, but the difference between setting this manually back to an empty string and using reset is if you had any kind of default value inside of here, reset will set it back to the original value you had in here.
02:05
This is really useful when you are pre-populating data in here that you want to keep. You don't want to just set it back to an empty string. And you can actually see that when you look at the profile section
02:18
of Laravel Breeze. So just as a really quick example, let's head over to the profile update form and let's take a look at what we get here.
02:29
So you can see we've got a form in here which contains the user's name and the user's email by default. When this form gets submitted, we don't want to clear out the name and email
02:39
because that would, if we head over to the profile section, that would get rid of the name and the email after the user had hit save. So instead, by resetting this,
02:48
it will set it back to the default values or the new values once they get reloaded. Okay, so let's go ahead and set this to ABC. Just see how that actually looks in practice
03:00
and then we're pretty much done. So this says ABC. I'm going to get rid of it and change that out. Hit post and there we go.
03:06
You can see it changes back to the original value. So as well as being a lot cleaner, this is also really helpful if you have any default values inside of your forms.

Episode summary

In this episode, we're tackling a common issue you'll notice when working with forms: after submitting, the text you've typed sticks around rather than clearing itself out. This happens because we're not doing a full page reload; we're just re-rendering the component. So, your form's local state doesn't automatically reset.

To fix this, we explore the different ways you can reset your form fields once the form is submitted. First, we look at the straightforward approach you might use—just manually setting your form data (like form.body = ''). While this clears the input, it's a bit of a pain if your form has more than one field or if you have default values you want to keep.

That's where Inertia's reset helper comes in! Instead of manually resetting everything, you can simply call reset() on your form, and it will restore all form fields to their initial state. Super handy, especially if you have forms with some pre-filled data (like updating a user's profile). We check out how this works in practice using Laravel Breeze's profile update form as an example, showing how the reset method keeps default values intact instead of just blanking everything out.

So by the end of the episode, you'll see how using Inertia's methods not only keeps your forms tidier but also makes handling defaults and form resets much easier.

Episode discussion

No comments, yet. Be the first!