1 min read

Handling Form Validation in Laravel

Form validation is a crucial part of any web application to ensure that the data received from users is correct and secure. Laravel provides several ways to handle form validation easily and effectively. Let’s explore these approaches with examples in simple terms.

 

Using Request Validation

 

One of the most common methods to handle form validation in Laravel is by using Request Validation.

1. Create a Form Request:

You can generate a custom request class using the Artisan CLI.


php artisan make:request StoreUserRequest


This command creates a new file in the `app/Http/Requests` directory.

2. Define Validation Rules:

Open the `StoreUserRequest.php` file and define your validation rules in the `rules` method.


<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreUserRequest extends FormRequest
{
public function authorize()
{
return true;
}

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



3. Use the Form Request in Your Controller:

 

In your controller, type-hint the request class in the method that handles the form submission.

 

<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreUserRequest;
use App\Models\User;

class UserController extends Controller
{
public function store(StoreUserRequest $request)
{
$validated = $request->validated();
User::create($validated);

return redirect()->back()->with('success', 'User created successfully!');
}
}


Using the Validator Facade

 

Another approach is using the `Validator` facade directly in your controller.

1. Define Validation Rules:

In your controller method, you can use the `Validator` facade to define and apply validation rules.

 

 

<?php

namespace App\Http\Controllers;

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

class UserController extends Controller
{
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users,email',
'password' => 'required|string|min:8|confirmed',
]);

if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}

User::create($validator->validated());

return redirect()->back()->with('success', 'User created successfully!');
}
}



Inline Validation

 

You can also perform validation inline within your controller without using a separate request class or the `Validator` facade.

 

1. Validate Directly in the Controller:

 

Use the `validate` method available on the `Request` object to validate the incoming data.

 

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;

class UserController extends Controller
{
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users,email',
'password' => 'required|string|min:8|confirmed',
]);

User::create($validated);

return redirect()->back()->with('success', 'User created successfully!');
}
}


Custom Error Messages

 

You can customize the error messages for validation rules in any of the above methods.

 

1. In Form Request:

 

Add a `messages` method to your form request class.

 

public function messages()
{
return [
'name.required' => 'We need to know your name!',
'email.required' => 'An email address is required!',
'email.unique' => 'This email is already taken!',
'password.required' => 'Please enter a password!',
];
}


2. In Validator Facade:

 

Pass an array of custom messages as the third parameter to the `make` method.

 

$validator = Validator::make($request->all(), [
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users,email',
'password' => 'required|string|min:8|confirmed',
], [
'name.required' => 'We need to know your name!',
'email.required' => 'An email address is required!',
'email.unique' => 'This email is already taken!',
'password.required' => 'Please enter a password!',
]);

 

3. In Inline Validation:

 

Pass an array of custom messages as the second parameter to the `validate` method.

 

$validated = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users,email',
'password' => 'required|string|min:8|confirmed',
], [
'name.required' => 'We need to know your name!',
'email.required' => 'An email address is required!',
'email.unique' => 'This email is already taken!',
'password.required' => 'Please enter a password!',
]);


 

Laravel offers multiple ways to handle form validation, making it easy to ensure your data is accurate and secure. Whether you prefer using form requests, the `Validator` facade, or inline validation, Laravel has you covered. You can also customize error messages to make them more user-friendly. By leveraging these validation methods, you can build robust and user-friendly applications.

Leave a Reply

Your email address will not be published. Required fields are marked *