Laravel 5.8 Form Validation with Example

“Laravel 5.8 Form Validation with Example,Create Form Validation in Laravel 5.8,Form Validation With Laravel 5.8”

Hello friends today I will tell you about the laravel 5.8 form validation via exsperts php tutorial how to validation in any registration form and blog form in laravel 5.8.

Now I will tell you step to step how laravel 5.8 form validation.

Step 1 :- First I will create a file of error-notification.blade.php name and I will add related code to error validation. And we will save this file in the layout name folder.

resources/views/layout/error-notification.blade.php

@if( Session::has('errors') )
<div class="alert alert-danger" role="alert" align="left">
<div class="errlist">
@foreach($errors->all() as $error) 
{{$error}}</br>
@endforeach
</div>
</div>
@endif

@if( Session::has('message') )
<div class="alert alert-success" role="alert">
{{ Session::get('message') }}
</div>
@endif

@if( Session::has('error') )
<div class="alert alert-danger" role="alert">
{{ Session::get('error') }}
</div>
@endif

Step 2 :- After that, we will create a blog form and it will take 2 fields title, description.

resources/views/blog.blade.php

<!DOCTYPE html>
<html>
<head>
<title>Insert,Update and Delete Create in Laravel 5.8 with Form Validation</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
</head>
<body>
@include('layout.error-notification')
<form action="{{url('blogsave')}}" method="post">
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<div class="col-md-12">
<div class="form-group">
<label for="">Title</label>
<input name="title" type="text" value="{{old('title')}}" class="form-control">
</div>
<div class="form-group">
<label for="">Description</label>
<textarea class="form-control" value="{{old('description')}}" rows="10" cols="105" name="description"></textarea>
</div>
</div>
<div class="col-md-6">
<div class="box-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</div>
</form>
</body>
</html>

Step 3:- Then after that we will add post action method url to the route file.

Route/web.php

Route::post('blogsave', 'blogController@blogsave');

Step 4:- Then we will create a controller and in this controller we will write the code of form validation. For validation, the code will be written in laravel 5.8. You can see this in the controller.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use DB;
use Validator;
use Redirect;
use View;
class blogController extends Controller
{
public function blogsave(Request $req)
{
// validation code
$this->validate($req, [
'title' => 'required',
'description' => 'required',
]);
// validation code
$title = $req->title;
$description = $req->description;
DB::table('blog')->insert([
'title' => $title,
'description'=> $description 
]);
return redirect()->back()->with('message', 'Successfully Save Your Blog.');
}
}