![]() But for this simplicity you're not using some powerful features that come included with the form request object, this is why my primary method is to use a Form Request object. With all these options on validating a request and handling your data, which one should you use? The simplest option is to just call this directly in your controller using the validate() method on the request object. ![]() This method will grab all the options in the rules array and pass this into the only() method to only return the data that was validated correctly. There is a third method that not many people are aware of, this is the validated() method, this is the best option of the three choices as it will return all data points that have been validated from your form requests. This is where the only method comes into play, this will accept an array of the parameters that you want to return and it will only return this data from the request object. This makes it very easy to get all the data you need but because it will just grab everything it will also bring back data that you don't need. All will return all the data in the request object. From the $request object you'll have access to two main methods all() and only(). Now we know that the request is valid we can use the request data in our method there are a few ways we can do this. Public function store(CreatePost $request) With this object we can now use this in our store method and on injecting the request object Laravel will automatically validate the request which makes sure everything is valid inside the method, therefore now the store method will look like this. Use Illuminate\Foundation\Http\FormRequest Therefore the Form Request object will look something like this. * Get the validation rules that apply to the request. Then in the rules method you can add the rules for your blog post. Return $this->user()->can('create-posts') * Determine if the user is authorized to make this request. The authorise method can be used to make sure that the current logged in user has the rights to create new posts. On this request object you'll get 2 methods by default authorize and rules. This will create a new object inside the folder App\Http\Requests. You can create new request objects by using the artisan command php artisan make:request. The other option to use on your validation is to use form request objects. It works in a similar way to the above but it's a bit nicer to read. On the object Illuminate\Http\Request, there is a validate() method that you can use to validate the data inside the object. # Using The Validate Method On The Request In Laravel 5.5 a feature was added to return the valid data from the validate method. Now you'll be able to pass in the Request object and the rules you want this request to follow to return the valid data. To use the validate method in your controllers you need to add the ValidatesRequests trait to your controller or your base controller. } Using Validate Method On Your Controller Then on submit of the form we need to put this to the update method. To update the blog post we're going to use the edit method to display the edit view of the existing blog post. ![]() ![]() Then when we submit the form we need to post this to the store method. To create a new blog post we're going to use the create method to display the view of the new blog post form. This is going to the have the 4 methods we need to create a new blog post and methods to handle the editing an existing blog post. In this example we're going to work within a controller for creating posts on a blog. ![]() In this tutorial we're going to show how you can use both of these approaches in your application. The other option is to use a Form Request object to validate the request automatically on any methods you choose. The simplest option is to use the validate() method on your controller, or you can use the validate() method on the request object itself. What Are The Different Options?Īs I said there are multiple ways we can validate the request on creating or updating a model so what are these options. In this tutorial we're going to investigate the different ways you can achieve this in Laravel and I'll go through my opinion on the best approach to take when validating requests. When it comes to validation in Laravel there are several ways you can go about this. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |