Laravel Form Validation with Error Messages Example

In this tutorial, you will learn Laravel Form Validation with Error Messages Example step by step process. Laravel provides a powerful validation system that includes customizable error messages. Using In Laravel, you can easily validate form input data and display error messages to the user using Laravel’s built-in form validation feature.

Here’s an example of how to use Laravel form validation with error messages.

Displaying Error Messages in laravel

First, you need to will create two routes one for showing form and another is displaying error messages in Laravel.

Step 1: Make Routes

use App\Http\Controllers\PostController;

Route::get('posts', 'PostController@create');
Route::post('posts', 'PostController@store')->name('posts');

Step 2: Create Controller

Now we will generate a new controller to handle the validation error messages request and showing them:

php artisan make:controller PostController

Now open your new generated controller App\Http\Controllers\PostController and put the below code on it:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
     * Show the application dashboard.
     * @return \Illuminate\Http\Response
    public function create()
        return view('posts');
     * Show the application dashboard.
     * @return \Illuminate\Http\Response
    public function store(Request $request)
           'title' => 'required|unique:posts|max:255',
           'discription' => 'required',
        return back()->with('success', 'Post created successfully.');

Step 3: Create Blade File

Next, we need to create a posts.blade.php blade view file for showing the form.

Now open the resources/views/posts.blade.php file and put the below code on it.

<!DOCTYPE html>
    <title>Laravel form validation example -</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="//" rel="stylesheet">
    <div class="container">
        <h1>Laravel form validation Example</h1>
        <div class="alert alert-success">
            {{ Session::get('success') }}
        <form method="POST" action="{{ route('posts') }}">
            {{ csrf_field() }}
            <div class="form-group">
                <input type="text" name="title" class="form-control" placeholder="Name">
                @if ($errors->has('title'))
                    <span class="text-danger">{{ $errors->first('title') }}</span>
            <div class="form-group">
                <textarea name="discription" class="form-control" placeholder="Discription"><textarea>
                @if ($errors->has('discription'))
                    <span class="text-danger">{{ $errors->first('discription') }}</span>
            <div class="form-group">
                <button class="btn btn-success btn-submit">Submit</button>

Learn Here: How to Show Input Boxes to Red Laravel Validations Example

Now you can check the validations errors in laravel blade file. For more follow us on twitter.

1 thought on “Laravel Form Validation with Error Messages Example”

Leave a Comment