agyanadda@gmail.com

Laravel Form Validation

Forms, Security and APIs 1 min read Updated Dec 2, 2026 Topic 19 of 31

Never trust user input. Laravel makes validation short and readable.

validate() in a controller

$request->validate([...]) checks the input. If a rule fails, Laravel redirects back automatically with the errors and the old input. If all rules pass, it returns only the validated data.

Common rules

  • required, nullable
  • string, integer, numeric, boolean
  • email, url, date
  • min:3, max:255, between:1,10
  • unique:users,email — value must not exist in the table
  • confirmed — needs a matching password_confirmation field
  • image, mimes:jpg,png, max:2048 (KB) for uploads

Showing errors in Blade

  • @error('email') <span>{{ $message }}</span> @enderror
  • value="{{ old('email') }}" refills the field after a failed submit

Form Requests

For bigger forms, run php artisan make:request StorePostRequest, put the rules in its rules() method, and type-hint it in the controller. The controller stays clean.

Example

Example — Register form validation PHP
<?php
public function store(Request $request)
{
    $data = $request->validate([
        'name'     => 'required|string|max:100',
        'email'    => 'required|email|unique:users,email',
        'password' => 'required|min:8|confirmed',
    ]);

    // only runs if every rule passed
    User::create($data);

    return redirect('/login')->with('success', 'Account created');
}

/* Blade:
<input name="email" value="{{ old('email') }}">
@error('email')
    <span class="text-danger">{{ $message }}</span>
@enderror */

If the email is already registered, the user is sent back to the form with "The email has already been taken." under the field and their other input still filled in.

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which rule checks that a value does not already exist in a table?

Exercise 2 True / False

When validate() fails, Laravel redirects back to the form automatically.

Exercise 3 Fill in the Blank

Use ___('email') in a Blade input to refill it after a failed submit.

Frequently Asked Questions

Pass a second array to validate() with custom messages, for example ['email.unique' => 'This email is already registered.'], or override the messages() method in a Form Request. Default messages are in the lang/en/validation.php file after you run php artisan lang:publish.