How to make custom validations in laravel 9 step by step guide.

Validation is a critical aspect of any web application, and Laravel provides several methods to validate incoming request data. In this article, we have explored three different methods to validate data in Laravel: using Form Request validation, using the Validator facade, and using the validation method in the controller. Each of these methods has its own advantages and can be used depending on the specific requirements of your application.

Using Form Request Validation

  1. Create a new Form Request class using the following artisan command:
php artisan make:request MyValidationRequest

Replace MyValidationRequest with the name of your choice.

2. Open the newly created file located in app/Http/Requests/MyValidationRequest.php

3. Define the validation rules for your input fields in the rules() method:

public function rules() { return [ 'name' => 'required|max:255', 'email' => 'required|email|unique:users', 'password' => 'required|min:8|confirmed', ]; }

This example defines validation rules for name, email, and password fields.

4.Use the newly created Form Request class in your controller method like this:

public function messages() { return [ 'name.required' => 'Please enter your name', 'email.required' => 'Please enter your email', 'email.email' => 'Please enter a valid email', 'email.unique' => 'This email is already taken', 'password.required' => 'Please enter a password', 'password.min' => 'Password must be at least 8 characters', 'password.confirmed' => 'Passwords do not match', ]; }

5. Use the newly created Form Request class in your controller method like this:

public function store(MyValidationRequest $request) { // Code to store data in database }

The MyValidationRequest class will automatically validate the incoming request before it reaches the controller method. If the validation fails, Laravel will return a JSON response with the errors.

Using Validator Facade

1. Import the Validator facade at the top of your controller:

use Illuminate\Support\Facades\Validator;

2. Create a new Validator instance and define your validation rules:

$validator = Validator::make($request->all(), [ 'name' => 'required|max:255', 'email' => 'required|email|unique:users', 'password' => 'required|min:8|confirmed', ]);

3. Optionally, you can define custom error messages for each rule by adding a messages() method to the class:

$validator->setCustomMessages([ 'name.required' => 'Please enter your name', 'email.required' => 'Please enter your email', 'email.email' => 'Please enter a valid email', 'email.unique' => 'This email is already taken',      'password.required' => 'Please enter a password',       'password.min' => 'Password must be at least 8 characters',      'password.confirmed' => 'Passwords do not match', ]);

4. Check if the validation fails and return a response with errors if it does:

if ($validator->fails()) { return response()->json([ 'success' => false, 'errors' => $validator->errors(), ], 422); }

Using Validation Method in Controller

1. Import the Illuminate\Support\Facades\Validator facade at the top of your controller:

use Illuminate\Support\Facades\Validator;

2. Define your validation rules:

$rules = [ 'name' => 'required|max:255', 'email' => 'required|email|unique:users', 'password' => 'required|min:8|confirmed', ];

3. Define your custom error messages.

4. Use the Validator facade to validate the request data:

$validator = Validator::make($request->all(), $rules, $messages); if ($validator->fails()) { return response()->json([ 'success' => false, 'errors' => $validator->errors(), ], 422); }

In this method, you define the validation rules and custom error messages in the controller method itself, and then use the Validator facade to perform the validation. If the validation fails, you can return a response with the errors. This method is useful for simple validations that don’t require a lot of custom logic, but it can quickly become unwieldy for complex validations with many rules.

Conclusion

Laravel provides a robust validation system that helps you ensure that the data entered by users is valid and meets the requirements of your application. The three methods we have covered in this article: Form Request validation, Validator facade, and validation method in the controller, all provide effective ways to validate data. By choosing the right method for your application, you can ensure that your users enter valid data, improving the overall user experience and the reliability of your application.

Comments