This episode is for members only

Sign up to access "Learn Inertia" right now.

Get started
Already a member? Sign in to continue
Playing
11. Clearing validation errors

Transcript

00:00
The errors that get attached to our form are also modifiable. So you can set these manually if you need to. For example, if you're using a client side validation library,
00:10
you can manually set these errors directly on the form. We'll have a look at a quick example of that now. But what we also want to do is maybe when we have a validation error and we click back into the text area, we want to automatically clear this.
00:24
So it's kind of out the way, ready for the user to submit again. So let's take a look at some of the methods that we can use on our form here to modify errors. So for example, the first thing that we can do is set an error. So I could set the body error to we need a body.
00:45
And just by doing that inside of there, you can see that stuck by default, we have that error in there. So you can lean on any of these methods to control all of the errors within your form. This is basically just an object with properties in,
00:59
but Inertia gives you all of these methods so you can conveniently do that. Now, let's look at a more practical example in the context of what we're building. And like I said, when we hit this and we get an error, maybe when we refocus on this text area, we just want to clear out all the errors for this.
01:16
Well, how do we do that? Let's go down to our text area here and let's add a on focus event handler to this. And now even directly from our template, we can go ahead and clear out the errors here so we can reference the form because it's available within our template
01:34
and we can just say clear errors. That's as simple as it gets. You can also clear individual errors. So if you just want to clear a specific thing that exists within your form,
01:44
like the body, if you had other data that you didn't want to clear, you can go ahead and pass these in directly. So let's go over and try this out. I'm going to hit post.
01:54
The body field is required. When I click back into that, that then, of course, calls that clear errors method over on our form and it gets rid of it for us. Now, there are loads of other things that you can do directly within forms.
02:05
I'm not going to go through everything because now that you've got the basics, everything else is in the inertia docs, all of the specifics. So if you have any specific use cases, you can reference the inertia docs to see what else you can do directly within these forms.
28 episodes2 hrs 20 mins

Overview

Single-page applications are great, but they come with the challenge of maintaining two separate projects — the client and the API.

Inertia serves as the solution, seamlessly connecting these two components and enabling you to build everything in one application (a monolith) while achieving the same outcome. The best part? You won't have to create an API.

To be honest, when I first started working with Inertia, I was a little confused about how everything worked. If that sounds familiar, then this course is made for you. I'll guide you through everything you need to know to kickstart your journey in building single-page applications using Laravel, Inertia, and Vue.

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

Comments

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