How is the validation done in the laravel framework?You will be told in this tutorial.Validation in the laravel framework is very easy.In laravel, when we post any form without completing any field, we get the value through the request method.If we get value null then validation is used so that the database does not have null value store.
Now we will apply validation in laravel. You can understand step by step.
register.blade.php
<!DOCTYPE html> <html> <head> <title>Insert,Update and Delete Create in Laravel</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css"> </head> <body> @include('error_notification') <form action="{{url('savedata')}}" 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>
After creating this form, you will hit url on your routes/web.php file by post method.
routes/web.php
<?php Route::post('savedata', 'DataController@savedata');
Now you will create a file of validation in which we will define this error message to your error message. Your resources/views/error_notification.blade.php will be located in this path.
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
This file means error message that will show you which field you have filled and which field has not filled out this file. This file should be inside your views folder. You can create a file of error_notification.blade.php and in that file the code is written above. Just paste and save in the same file and place this file in the error folder inside your views folder.
Now you create a controller file named DataController.php in which we write the code of validation.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use DB; use Redirect; use Validator; use View; class DataController extends Controller { public function savedata(Request $request) { $title = $request->title; $description = $request->description; $inputs = [ 'title' => $title, 'description' => $description, ]; $rules = [ 'title' => 'required', 'description' => 'required', ]; $messages = [ 'title.required' => 'Please enter Title 'description.required' => 'Please enter Description ]; $validation = Validator::make($inputs, $rules, $messages); if( $validation->fails() ){ return redirect()->back()->withInput()->with('errors', $validation->errors() ); } DB::table('blog')->insert([ 'title' =>$title, 'description' =>$description ]); return redirect()->back()->with('message', 'Thank you for Post Blog.'); } }