Playing
21. Refactoring validation

Transcript

00:00
One thing that I really love to refactor is moving out validation rules from our controller into a form request.
00:07
This completely tidies it up and makes our controllers really, really tidy. Now, as a side effect of doing this, we're actually going to be refactoring the way that authorization is handled as well because we can authorize within a form request. So we can get rid of these two lines, move them both over to a form request,
00:24
and of course, because our tests check both things, we can see if everything is still working without having to touch our app. So we will start with our book put controller. So let's go ahead and create out a form request for this.
00:40
Now, we could create a shared form request for putting and storing a book, but let's keep these separate for now just in case the rules change for putting and storing books, which is likely in the future. So we're going to go ahead and make a request in here, and we're going to say book put request,
01:00
or you could say book put form request. We tend to keep this shorter. So let's go over to our book put request, and if you're new to Laravel or form requests, let's take a look. So authorized is going to be where our authorization rule goes,
01:17
and rules are going to be where our validation rules go. So let's go over here, grab our validation rules here, get rid of this line, and put these into here. So that's pretty much that done. Let's make sure we pull our namespaces in, and then we can move this over.
01:35
So if we remember earlier over in our book put test, what we did is we got rid of this data. That's now not going to work because at this point of the request, when we switch this out, the validation is going to come first before the authorization. So we're going to go ahead and put this back in just temporarily.
01:55
We can remove it in a minute, and let's switch this to book put request. Okay, so I'm going to run PEST, and we get a failure. Let's have a look. So it looks like this isn't being updated, and the reason for that is if we come over to our book put request,
02:11
authorization by default is set to false. Let's switch that to true because we're handling our authorization just here. And let's rerun PEST, and great, we get green. So that validation was successfully refactored.
02:25
So let's go ahead and create a form request for our book store controller, and move these validation rules out as well. And then we'll come back and look at how we can tidy this up as well. And of course, run our test to make sure.
02:39
So again, we're going to grab these validation rules here, get rid of this validation here, and we're going to make another form request, and this is going to be book store request. Now, in terms of our book store request, we know, if we just open this up properly, that we don't need to authorize this.
02:59
The user needs to be signed in, but that is handled by our middleware, so we can just explicitly set that to true. And we can put our rules back in here, and come back over to our book store controller,
03:12
and switch this to a book store request. Let's run PEST again, and we get green, great. Okay, so specifically for our book put request, and our book put controller, we want to move this authorization over to that form request.
03:28
Now, to do this, we need to do this in a slightly different way. Now, of course, if we get rid of this authorization here, and rerun PEST, we are going to get a failure, because we don't get a 403, we get a 302. So, if we come over to our book put request, let's slightly differ the way we authorize this.
03:49
So, because we're dealing with a request, technically a request object, we can go ahead and say this user, which is the same as saying request user in our controller, and we can say can update, and where do we get our book from? Well, once again, because this is a request, and we're using root model binding,
04:12
we actually get this book in our request object. So, that should be enough to now be a successful refactor. So, we have them test that back things up, and now we have a really nice, neat controller with all of our logic to authorize and validate tucked away.

Episode summary

In this episode, we're tackling some much-needed refactoring to clean up our controllers—specifically, we're moving validation and authorization logic out of the controller and into form request classes. It might sound dry, but this process does wonders for making controllers clear and maintainable.

We start by extracting validation logic from our "BookPutController" into a dedicated form request, making it clear where our rules live. A bonus of this is that we can handle authorization inside the same form request as well, resulting in super-tidy controllers. While doing this, we double-check our tests to ensure everything's still running as expected after the changes.

Next, we do a similar refactor for the "BookStoreController," creating a separate form request for storing books. We discuss why keeping 'put' and 'store' rules in their own classes, for now, makes future changes easier. Along the way, we see that Laravel's request objects give us everything we need, including root model binding, which makes authorization inside form requests straightforward.

By the end of the video, we've got controllers that are much easier to understand, with validation and authorization logic consolidated in form requests. We wrap things up by re-running our tests to confirm everything works perfectly after refactoring. If you want cleaner controllers and better-organized code, this episode shows you exactly how to achieve that!

Episode discussion

No comments, yet. Be the first!