0
0
LaravelHow-ToBeginner · 3 min read

How to Use Email Rule in Laravel Validation

In Laravel, use the email validation rule inside your request validation to check if a field contains a valid email address. You add it as a string in the rules array like 'email' => 'required|email' to enforce the format automatically.
📐

Syntax

The email rule is used in Laravel's validation rules array to verify that a given input is a valid email address format.

  • Field name: The name of the input field to validate.
  • Rules: A string or array of validation rules, including email.

Example syntax:

php
'email' => 'required|email'
💻

Example

This example shows how to validate an email input in a Laravel controller method using the email rule. It ensures the email field is present and properly formatted.

php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function store(Request $request)
    {
        $validated = $request->validate([
            'email' => 'required|email',
        ]);

        return response()->json(['message' => 'Email is valid!', 'email' => $validated['email']]);
    }
}
Output
{"message":"Email is valid!","email":"user@example.com"}
⚠️

Common Pitfalls

Common mistakes when using the email rule include:

  • Forgetting to include required if the field must not be empty, which allows empty strings to pass.
  • Using email alone without other rules may accept empty or null values.
  • Not handling validation errors properly, which can cause unclear feedback to users.

Example of wrong and right usage:

php
<?php
// Wrong: allows empty email
'email' => 'email',

// Right: requires email and validates format
'email' => 'required|email',
📊

Quick Reference

RuleDescription
requiredField must be present and not empty
emailField must be a valid email format
nullableField can be null or empty, but if present must be valid
unique:table,columnEmail must be unique in a database table column

Key Takeaways

Use 'required|email' to ensure the email field is present and correctly formatted.
The 'email' rule alone does not prevent empty values; combine with 'required' if needed.
Laravel automatically returns validation errors if the email is invalid.
Use additional rules like 'unique' to enforce email uniqueness in your database.
Always handle validation errors to give clear feedback to users.