Laravel is a popular PHP framework that provides many features to make web development easier and faster. One of these features is validation, which allows you to check the data that is sent to your application by the users and ensure that it meets your expectations.

Validation is important because it helps you to prevent errors, bugs, and security issues that may arise from invalid or malicious input. For example, if you have a form that asks the user to enter their email address, you want to make sure that the email address is valid and not empty before you send them a confirmation email.

Laravel provides several ways to validate request data, such as using the validate method, creating form request classes, or using custom validation rules. In this blog post, we will focus on the validation method, which is the simplest and most common way to validate request data in Laravel.

The validate method is available on any request object, such as $request or request(). It accepts an array of validation rules as the first argument, and optionally an array of custom error messages as the second argument. The validation rules are key-value pairs, where the key is the name of the input field and the value is a string or an array of validation rules.

For example, if you have a form that asks the user to enter their name, email, and password, you can use the following code to validate the request data:

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function __invoke(Request $request)
    {
         // Validate the request data

         $validated = $request->validate([
                'name' => 'required|string|max:255',
                'email' => 'required|email|unique:users',
                'password' => 'required|string|min:8|confirmed',
         ]);

         // If validation passes, use the validated data
         // If validation fails, redirect back with errors
    }
}

 

The validate method will automatically redirect back to the previous page with errors if the validation fails. The errors will be stored in a $errors variable that you can access in your views. You can also use the @error directive to display specific error messages for each field.

For example, if you want to display an error message below each input field in your form, you can use the following code in your blade template:

<form method="POST" action="/register">
   @csrf
   <div>
      <label for="name">Name</label>
      <input id="name" type="text" name="name" value="{{ old('name') }}">
      @error('name')
      <span>{{ $message }}</span>
      @enderror
   </div>
   <div>
      <label for="email">Email</label>
      <input id="email" type="email" name="email" value="{{ old('email') }}">
      @error('email')
      <span>{{ $message }}</span>
      @enderror
   </div>
   <div>
      <label for="password">Password</label>
      <input id="password" type="password" name="password">
      @error('password')
      <span>{{ $message }}</span>
      @enderror
   </div>
   <div>
      <label for="password_confirmation">Confirm Password</label>
      <input id="password_confirmation" type="password" name="password_confirmation">
   </div>
   <div>
      <button type="submit">Register</button>
   </div>
</form>

 

The validate method also returns an array of validated data that you can use in your controller or model. For example, if you want to create a new user with the validated data, you can use the following code:

<?php

// Validate the request data

$validated = $request->validate([
    'name' => 'required|string|max:255',
    'email' => 'required|email|unique:users',
    'password' => 'required|string|min:8|confirmed',
]);

// Create a new user with the validated data
$user = User::create($validated);

 

Conclusion

As you can see, validating request data in Laravel is very easy and convenient with the validate method. You can also check out the official documentation for more details and examples of validation rules and custom error messages.

I hope this blog post was helpful for you. If you have any questions or feedback, please leave a comment below. Thank you for reading! blush