How to Create and Use Custom Request for smart validation in Laravel
Today we will learn about laravel form requests e.i custom request classes. I will show you how to create and use laravel custom request class. For more complex validation scenarios we use form requests. To create a form request class we will use the make:request artisan command.
Let's run the below code to create a request class:
php artisan make:request StoreProductRequest
Aafter executing the above command Requests directory will create inside app\Http directory. Also create StoreProductRequest class inside app\Http\Requests directory. This request class will contains the validation code script. Each form request generated by Laravel has two methods: authorize and rules
StoreProductReques.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreProductRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
*/
public function rules(): array
{
return [
//
];
}
}
Now we will see how to use request class to validate the form request. Lets see the following controller validation example code. We will move this validation code in StoreProductRequest class and clean the controller.
StoreProductController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class StoreProductController extends Controller
{
public function store_new_product(Request $request)
{
$request->validate([
'title' => 'required|min:20|max:100',
'slug' => 'required|max:191|unique:projects,slug',
'description' => 'required|min:50|max:50000',
'image' => 'required|mimes:jpg,jpeg,png,bmp,tiff,svg|max:1024',
'category' => 'required',
]);
//execute rest of the code....
}
}
Now update the StoreProductRequest class with the below code and remove the validation from the controller. This way we can use custom request class to create complex validation.
StoreProductRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreProductRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'title' => 'required|min:20|max:100',
'slug' => 'required|max:191|unique:projects,slug',
'description' => 'required|min:50|max:50000',
'image' => 'required|mimes:jpg,jpeg,png,bmp,tiff,svg|max:1024',
'category' => 'required',
];
}
public function messages()
{
return [
'title.required' =>'Site title is required',
'slug.required' => 'Slug is required',
'description.required' => 'Description is required',
'image.required' => 'Image is required',
'category.required' => 'Category is required',
];
}
}
Finally we have to update the controller store_new_product method with the StoreProductRequest like below.
StoreProductController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class StoreProductController extends Controller
{
public function store_new_product(StoreProductRequest $request)
{
//execute rest of the code....
}
}
This way we can create and validate code using custom requests class.
If you like what you are reading, please think about buying us a coffee as a token of appreciation.
We appreciate your support and are committed to providing you useful and informative content.
We are thankful for your ongoing support
You May Like These:
Laravel 10 multiple form validation on the same page-WebJourney
Laravel 9 ajax form validation and display error messages