Laravel 10 Form Validation Example

Websolutionstuff | Mar-22-2023 | Categories : Laravel

In this article, we will see the laravel 10 form validation example. Here, we will learn about basic form input with laravel validation. Laravel provides several different approaches to validate your application's incoming data.

For any incoming data, we need to validate it before storing it in the database. It is most common to use the validate method available on all incoming HTTP requests.

So, let's see how to validate a form in laravel 10, form validation in laravel 10, laravel 10 validation, custom validation in laravel 10, and custom laravel validation message.

Step 1: Install Laravel 10

In this step, we will install the laravel 10 application using the following command.

composer create-project laravel/laravel laravel_10_form_validation


Step 2: Add Route

In this step, we will add routes to the web.php file.



use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!

Route::get('/', function () {
    return view('welcome');

Route::get('index', [ UserController::class, 'index' ]);
Route::post('store', [ UserController::class, 'store' ])->name('store');



Step 3: Create UserController

Now, we will create UserController using the following command.

php artisan make:controller UserController



namespace App\Http\Controllers;

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

class UserController extends Controller
    public function index(){
        return view('index');

    public function store(Request $request){
        $validatedData = $request->validate([
            'name' => 'required',
            'password' => 'required|min:5',
            'confirm_password' => 'required|same:password|min:5',
            'email' => 'required|email|unique:users'
        ], [
            'name.required' => 'Name field is required.',
            'password.required' => 'Password field is required.',
            'confirm_password.required' => 'Confirm password field is required.',
            'email.required' => 'Email field is required.',
            '' => 'Email field must be email address.'
        $validatedData['password'] = bcrypt($validatedData['password']);
        $user = User::create($validatedData);
        return back()->with('success', 'User created successfully.');

In laravel 10 Alternatively, validation rules may be specified as arrays of rules instead of a single | delimited string.

$validatedData = $request->validate([
    'name' => 'required',
    'password' => ['required', 'min:8'],
    'email' => ['required, email, unique:users'],



Stopping On First Validation Failure

Sometimes we need to stop running validation rules on an attribute after the first validation failure. To do so, assign the bail rule to the attribute in laravel 10.

    'name' => 'bail|required|unique:users|max:255',
    'password' => ['required', 'min:8'],
    'email' => ['bail, required, email, unique:users'],

In this example, if the unique rule on the name attribute fails, the max rule will not be checked. Rules will be validated in the order they are assigned. 


Step 4: Add Blade File

In this step, we will create an index.blade.php file.


<!DOCTYPE html>
        <title>Laravel 10 Form Validation Example - Websolutionstuff</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="[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
        <div class="container col-md-6">    
            <h3 class="mt-5">Laravel 10 Form Validation Example - Websolutionstuff</h3>       
            <div class="alert alert-success">
                {{ Session::get('success') }}
            <!-- Way 1: Display All Error Messages -->
            @if ($errors->any())
                <div class="alert alert-danger">                
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
            <form method="POST" action="{{ route('store') }}">      
                {{ csrf_field() }}      
                <div class="mb-3">
                    <label class="form-label" for="inputName">Name:</label>
                    <input type="text" name="name" class="form-control @error('name') is-invalid @enderror"  placeholder="Name">
                    <!-- Way 2: Display Error Message -->
                        <span class="text-danger">{{ $message }}</span>

                <div class="mb-3">
                    <label class="form-label" for="inputEmail">Email:</label>
                    <input type="text" name="email" class="form-control @error('email') is-invalid @enderror" placeholder="Email">
                        <span class="text-danger">{{ $message }}</span>
                <div class="mb-3">
                    <label class="form-label" for="inputPassword">Password:</label>
                    <input type="password" name="password" class="form-control @error('password') is-invalid @enderror" placeholder="Password">
                    <!-- Way 3: Display Error Message -->
                    @if ($errors->has('password'))
                        <span class="text-danger">{{ $errors->first('password') }}</span>

                <div class="mb-3">
                    <label class="form-label" for="inputPassword">Confirm Password:</label>
                    <input type="password" name="confirm_password" class="form-control @error('confirm_password') is-invalid @enderror" placeholder="Confirm Password">
                    <!-- Way 3: Display Error Message -->
                    @if ($errors->has('confirm_password'))
                        <span class="text-danger">{{ $errors->first('confirm_password') }}</span>
                <div class="mb-3">
                    <button class="btn btn-success btn-submit">Submit</button>






You might also like:

Recommended Post
Featured Post
Github And Git Commands
Github And Git Commands

GitHub, Inc. is a United States-based global company that provides hosting for software development and version control...

Read More


Autotab To Next Input Field JQuery
Autotab To Next Input Field JQ...

In this article, we will see how to focus on the next input when the max length is reached. Sometimes we have...

Read More


How To Create Web Notifications In Laravel 9 Using Pusher
How To Create Web Notification...

In this article, we will see how to create web notifications in laravel 9 using pusher. Here, we will learn how to...

Read More


How To Install LAMP Server In Ubuntu 22.04
How To Install LAMP Server In...

In today's digital landscape, hosting dynamic websites and powerful web applications is essential for individuals an...

Read More