Laravel 8 Form Validation Example Tutorial
https://laratutorials.com/laravel-8-form-validation-example-tutorial/
Laravel 8 Form Validation Example Tutorial
Laravel 8 form validation example tutorial. In this post, i will teach from scratch on how to validate and store form data in laravel 8 application.
In this post example, i will create add blog post form with validation and insert form data to database using laravel 8 version. As well as And validation form data before store into database.
How to Validate Form Data in Laravel 8
Step 1 – Install Laravel 8 Application
Step 2 – Configuring Database using Env File
Step 3 – Create Model & Migration File For Form
Step 4 – Create Routes
Step 5 – Creating Controller
Step 6 – Create Blade File For Form
Step 7 – Start Development Server
Step 8 – Run Laravel 8 Form Validation App On Browser
Step 1 – Install Laravel 8 Application
In step 1, open your terminal and navigate to your local web server directory using the following command:
Then install laravel 8 latest application using the following command:
Step 2 – Configuring Database using Env File
In step 2, open your downloaded laravel 8 app into any text editor. Then find .env file and configure database detail like following:DB_CONNECTION=mysqlDB_HOST=127.0.0.1DB_PORT=3306DB_DATABASE=db nameDB_USERNAME=db user nameDB_PASSWORD=db password
Step 3 – Create Model & Migration File For Form
In step 3, open command prompt and navigate to your project by using the following command:
Then create model and migration file by using the following command:
The above command will create two files into your laravel 8 form validation application, which is located inside the following locations:
LaravelForm/app/Models/Post.php
LaravelForm/database/migrations/2020_09_09_025857_create_posts_table.php
So, find create_posts_table.php file inside LaravelForm/database/migrations/ directory. Then open this file and add the following code into function up() on this file: public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('description'); $table->timestamps(); }); }
Now, open again your terminal and type the following command on cmd to create tables into your selected database:
Step 4 – Create Routes
In step 4, open your web.php file, which is located inside routes directory. Then add the following routes into web.php file:<?phpuse Illuminate\Support\Facades\Route;use App\Http\Controllers\PostController;/*|--------------------------------------------------------------------------| 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('add-blog-post-form', [PostController::class, 'index']);Route::post('store-form', [PostController::class, 'store']);
Step 5 – Creating Controller
In step 5, create form controller by using the following command:
The above command will create PostController.php file, which is located inside LaravelForm/app/Http/Controllers/ directory.
The following laravel validation rules will validate form data before store into database in laravel: $validatedData = $request->validate([ 'title' => 'required|unique:posts|max:255', 'description' => 'required', ]);
So open PostController.php file and add the following code into it:<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use App\Models\Post;class PostController extends Controller{ public function index() { return view('add-blog-post-form'); } public function store(Request $request) { $validatedData = $request->validate([ 'title' => 'required|unique:posts|max:255', 'description' => 'required', ]); $post = new Post; $post->title = $request->title; $post->description = $request->description; $post->save(); return redirect('add-blog-post-form')->with('status', 'Blog Post Form Data Has Been inserted'); }}
Step 6 – Create Blade File For Form
In step 6, create new blade view file that named add-blog-post-form.blade.php inside resources/views directory for add blog post form.
The following code display error message in laravel forms. So do not forget to add the following code along laravel forms fields: @error('title') <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div> @enderror
Don’t worry i have already added the validation error message display code along with each form fields.
So, you can add the following php and html form code into add-blog-post-form.blade.php:<!DOCTYPE html><html><head> <title>How to Validate Form Data in Laravel 8</title> <meta name="csrf-token" content="{{ csrf_token() }}"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"></head><body><div class="container mt-4"> @if(session('status')) <div class="alert alert-success"> {{ session('status') }} </div> @endif <div class="card"> <div class="card-header text-center font-weight-bold"> <h2>Laravel 8 Form Validation Example Tutorial</h2> </div> <div class="card-body"> <form name="add-blog-post-form" id="add-blog-post-form" method="post" action="{{url('store-form')}}"> @csrf <div class="form-group"> <label for="exampleInputEmail1">Title</label> <input type="text" id="title" name="title" class="@error('title') is-invalid @enderror form-control"> @error('title') <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div> @enderror </div> <div class="form-group"> <label for="exampleInputEmail1">Description</label> <textarea name="description" class="@error('title') is-invalid @enderror form-control"></textarea> @error('title') <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div> @enderror </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> </div></div></body></html>
Step 7 – Start Development Server
Finally, open your command prompt again and run the following command to start development server for your laravel 8 form application:
Step 8 – Run Laravel 8 Form Validation App On Browser
In step 8, open your browser and fire the following url into your browser:
Last updated