Hello Devs,

In this tutorial, we are going to learn laravel 7 form validation example.

Follow this step by step guide given below:




Step 1 : 


Install Laravel

composer create-project --prefer-dist laravel/laravel blog



Step 2 : 


Create Migration & Model 

php artisan make:model User -m

database/migrations/2019_12_11_111847_create_users_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email');
            $table->string('mobile_no');
            $table->string('password');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

Run this command:

php artisan migrate



Step 3 : 


Create Controller

php artisan make:controller UserController



Step 4 :


 Create Controller method app/http/controllers/UserController

<?php

namespace App\Http\Controllers;

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

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

    public function store(Request $request)
    {
        $input = $request->all();

        $request->validate([
            'name' => 'required',
            'email' => 'required|email',
            'mobile_no' => 'required|min:10|numeric',
            'password' => 'required|min:6'
        ]);

        User::create($input);

        return back()
                ->with('success','User Created Successfully');
    }
}



Step 5 : 


Create Routes routes/web.php

Route::get('form-validation','UserController@create')->name('user.create');
Route::post('form-validation','UserController@store')->name('user.store');



Step 6 : 


Create View File resources/views/create.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 7 Form Validation</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha256-L/W5Wfqfa0sdBNIKN9cG6QA5F2qx4qICmU2VgLruv9Y=" crossorigin="anonymous" />
</head>
<body class="bg-dark">
    <div class="container">
        <div class="row">
            <div class="col-md-6 offset-3 mt-5">
                <div class="card">
                    <div class="card-header">
                        <h5>Laravel 7 Form Validation - Rathorji.in</h5>
                    </div>
                    <div class="card-body">
                        @if (count($errors) > 0)
                            <div class="alert alert-danger">
                                <ul>
                                    @foreach ($errors->all() as $error)
                                        <li>{{ $error }}</li>
                                    @endforeach
                                </ul>
                            </div>
                        @endif

                        @if ($message = Session::get('success'))
                            <div class="alert alert-success alert-block">
                                <button type="button" class="close" data-dismiss="alert">×</button>
                                <strong>{{ $message }}</strong>
                            </div>
                        @endif

                        <form action="{{ route('user.store') }}" method="post" enctype="multipart/form-data">
                            @csrf
                            <div class="form-group">
                                <label><strong>Name : </strong></label>
                                <input type="text" name="name" class="form-control">
                            </div>
                            <div class="form-group">
                                <label><strong>Email : </strong></label>
                                <input type="email" name="email" class="form-control">
                            </div>
                            <div class="form-group">
                                <label><strong>Mobile No : </strong></label>
                                <input type="text" name="mobile_no" class="form-control">
                            </div>
                            <div class="form-group">
                                <label><strong>Password : </strong></label>
                                <input type="password" name="password" class="form-control">
                            </div>
                            <div class="form-group text-center">
                                <input type="submit" class="btn btn-success" name="submit" value="Save">
                            </div>
                        </form>                     
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>


I hope this example helps you.