Hello Laravel Friends,

Today I will be explaining How to Apply Form Validation in the Request Controller in Laravel.

Validation is the important aspect while taking data from users. In Laravel you can easily validate using a request controller. To perform validation you need to have the following requirements.

  • PHP version greater than 7.0
  • A fresh laravel project

Once you are done with the above requirements, you can start implementing the below steps.

Let’s start

Steps to Add Form Validation in the Request Controller in Laravel:

Here we took the user registration form as an example. Here we will validate the name, email, password, and confirm password fields in the request.

Step 1: First, we will create a route in the route/web.php file for user registration post requests. In this request, we will pass name, email, password, and confirm password as body parameters.

Step 2: Then after, we will create a User controller for getting requests for user registration post route. Execute the following command to create the UserController.php file in your project root directory.

The UserControlller.php file is now created in your project App/Http/Controller directory. 

Step 3: Now create userRegistration function 

Step 4: Then create the UserRequest.php file for the validation request. To create UserRequest.php execute the following command

The UserRequest.php file is created in your project App/Http/Request directory.

Step 5: For validating the request add rule in the rules function defined in the UserRequest.php file.

Step 6: If there is an incorrect input, an error will be generated. You can get an error message with the following code in your blade file.

Result:

After implementing the above code, validation has been successfully applied to the user registration form.

form validation

Conclusion:

This way you can easily validate form input in the request controller in Laravel. For any custom development Hire Laravel Developer.

Share this tutorial with other Laravel developers and stay updated to learn more with us.

Happy Coding!

Click to rate this post!
[Total: 7 Average: 4.3]