Form Validation In Laravel.
Validation may be a quite common task in web applications. Entered data should be Validated before sending it to server. This post deals with form validation in Laravel. After reading this post we’ll be ready to validate form with Laravel. Below are the steps to implement form validation in Laravel.
Several pre-define validation rules are available in Laravel. In this post I used some validation rules listed below that need to apply.
1) Required: | To make field a required. |
2) Min: | To set limit to enter minimum character. |
3) Max: | To set limit to enter maximum character. |
4) Numeric: | To allow only numeric value. |
5) String: | To allow only string value. |
Above seven rules are used in my examples but Laravel has many pre defined rules and we can use it.
So in this example I will create one page to create product details from scratch and apply validation rules.
Step1 : Add Routes.
In this first step we add routes .
routes/web.php
Route::resource('product','ProductController');
Step2 : Create ProductController
In this step we will create new resource controller names as ProductController and will write two method with validation rules. So first create new resourcer controller ProductController by using bellow command.
https://laravel.com/docs/7.x/controllers#resource-controllers
php artisan make:controller ProductController --resource
Add Below code in Controller
<?php namespace App\Http\Controllers; use App\Product; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; use App\Providers\RouteServiceProvider; class ProductController extends Controller { /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { return view('product.create'); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $validatedData = $request->validate([ 'name' => 'required|string|min:3', 'qty' => 'required|numeric', 'price' => 'required|numeric', 'model' => 'required|string', 'sku' => 'required|string', ]); return Product::create([ 'name' => $request['name'], 'qty'=>$request['qty'], 'price'=>$request['price'], 'model'=>$request['model'], 'sku'=>$request['sku'] ]); } }
Step3 : Create Blade View File And Add Below Code.
<div class="card-body"> <form method="POST" action=" {{route('product.store')}}"> @csrf <div class="form-row"> <div class="col-md-5"> <div class="form-group"><label>Name</label> <input class="form-control py-4" id="name" name="name" value="{{ old('name') }}" type="text" /> </div> @if ($errors->has('name')) <span class="text-danger"> <strong>{{ $errors->first('name') }}</strong> </span> @endif </div> </div> <div class="form-row"> <div class="col-md-5"> <div class="form-group"><label>Quantity</label> <input class="form-control py-4" id="qty" name="qty" value="{{ old('qty') }}" type="number" /> </div> @if ($errors->has('qty')) <span class="text-danger"> <strong>{{ $errors->first('qty') }}</strong> </span> @endif </div> <div class="col-md-5"> <div class="form-group"><label>Price</label> <input class="form-control py-4" id="price" name="price" value="{{ old('price') }}" type="number" /> </div> @if ($errors->has('price')) <span class="text-danger"> <strong>{{ $errors->first('price') }}</strong> </span> @endif </div> </div> <div class="form-row"> <div class="col-md-5"> <div class="form-group"><label>Model</label> <input class="form-control py-4" id="model" name="model" value="{{ old('model') }}" type="text" /> </div> @if ($errors->has('model')) <span class="text-danger"> <strong>{{ $errors->first('model') }}</strong> </span> @endif </div> <div class="col-md-5"> <div class="form-group"><label>SKU</label> <input class="form-control py-4" id="sku" name="sku" value="{{ old('sku') }}"type="text" /> </div> @if ($errors->has('sku')) <span class="text-danger"> <strong>{{ $errors->first('sku') }}</strong> </span> @endif </div> </div> <div class="form-group mt-6 mb-2"> <button type="submit" class="btn btn-primary"> {{ __('Submit') }} </button> </div> </form> </div>