This episode is for members only

Sign up to access "Laravel Actions" right now.

Get started
Already a member? Sign in to continue
Playing
05. Validating controller actions

Transcript

00:00
We're validating in this controller over here, but ideally if we want to use createPost as a
00:05
controller, we really want to be able to validate inside of here. Now what we can't do is something like this and validate because validate exists on the controller as a method. So what we're going to do is instead let Laravel Actions handle this by implementing a rules method and returning an array of rules. So let's go ahead and implement this now and see how it works. Now just to
00:32
demonstrate this beforehand, if we just go over here and hit create, you can see that we get an SQL error because the body is not nullable and of course we haven't filled it. We have no validation at the moment to stop that happening. So inside of our action we're going to create out a rules method which just returns an array. So again we can type in that if we want to. So let's return
00:53
an array in here with just the validation that we need. So we've got our body just in here and we're going to say that that's required and effectively what this is going to do is just take any of the keys from the request as you post this through to this action and it's going to validate the keys that exist within the request. Pretty straightforward and of course you can do
01:15
pretty much anything you want in here. You could add other rules in here separated by the pipe symbol. You could use custom rules in here. It's going to work in exactly the same way as normal Laravel validation. So now that we've got this rules array being returned by this method, let's go ahead and hit create and there we go. Sure enough our validation is still working. Now when
01:37
we validate over on a controller we have additional options that we can pass through to here to give the specific messages any custom attributes. We can do that within our actions as well. We just need to implement another method and if you're used to things like live wire this will be very natural to you anyway. So let's go ahead and create out here a getValidationMessages method.
02:02
Again this is going to return an array and here we can just customize the errors that we see. So in our case we're going to say body we're going to separate these by a dot which is just a Laravel convention. So you would do this anywhere that you're customizing validation messages and we're just going to say we need a body just to change this up a bit. So if we head over now and hit
02:23
create there we go we've got custom validation message in there. So as we have seen already using this as a controller with the asController method is very simple. We just call the handle method in here with the data that we need and we now know that we can use our validation in here. So this is pretty much now behaving like the controller that we created over here.
12 episodes1 hr 2 mins

Overview

Actions are single classes that do one thing. Laravel Actions is a package that allows you to run these classes as anything you want, whether it's a controller, listener, console command – or just on its own.

Keeping your app structure to single classes like this lets you focus on what your app does rather than the design decisions around controllers, listeners and commands.

Single actions are also easier to test, and we'll also cover that!

This course is for you if:

  • You'd like to try a fresh approach to structuring your apps
  • Your app shares logic, and you'd like to combine this logic into one class that runs anywhere
  • You've heard of (or used) Laravel Actions, and you'd like a run-through
Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!