Today, We want to share with you PHP Laravel Validation Example Tutorial From Scratch.In this post we will show you Laravel 5.6 FormRequests validation, hear for How to validate unique column when insert,List of Laravel validation rules we will give you demo and example for implement.In this post, we will learn about PHP Laravel 5.6 Validation example for register form with error messages with an example.
PHP Laravel Validation Example Tutorial From Scratch
There are the Following The simple About PHP Laravel Validation Example Tutorial From Scratch Full Information With Example and source code.
As I will cover this Post with live Working example to develop php – Laravel 5.6 form validation unique, so the Validation – Laravel – The PHP Framework for this example is following below.
Step 1 : Include Laravel Routes
routes/web.php
Route::get('register', '[email protected]'); Route::post('register','[email protected]');
Step 2 : Make a Laravel Controller
registerController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Member; //Laravel 5 Validation Example From Scratch class registerController extends Controller { public function create() { return view('register'); } public function store() { request()->validate([ 'name' => 'required|min:2|max:50', 'mobile' => 'required|numeric', 'email' => 'required|email|unique:members', 'password' => 'required|min:6', 'repeat_password' => 'required|min:6|max:20|same:password', ], [ 'name.required' => 'Member Name is required', 'name.min' => 'Member Name must be at least 2 characters.', 'name.max' => 'Member Name should not be greater than 50 characters.', ]); $input = request()->except('password','repeat_password'); $member=new Member($input); $member->password=bcrypt(request()->password); $member->save(); return back()->with('success', 'Member created successfully.'); } }
//bail rule to attribute 'email' => 'bail|required|email|unique:members' //nullable 'mobile' => 'nullable|numeric',
Step3 : make Laravel Blade View interface File
register.blade.php
<!DOCTYPE html> <html> <head> <title>Laravel 5.6 Validation With Custom Rules Example From Scratch - Pakainfo.com</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> </head> <body> <div class="container pakainfo"> <h3>Laravel 5.6 Validation With Custom Rules Example From Scratch</h3> @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"> <p>{{ $message }}</p> </div> @endif <form action="{{ url('register') }}" method="POST" id="signupForm"> {{ csrf_field() }} <div class="gst form-group {{ $errors->has('name') ? 'has-error' : '' }}"> <label class="span6 control-label">Name:</label> <input type="text" name="name" class="form-control" value="{{ old('name') }}"> @if ($errors->has('name')) <span class="text-danger">{{ $errors->first('name') }}</span> @endif </div> <div class="gst form-group {{ $errors->has('mobile') ? 'has-error' : '' }}"> <label class="span6 control-label">Mobile:</label> <input type="text" name="mobile" class="form-control" value="{{ old('mobile') }}"> @if ($errors->has('mobile')) <span class="text-danger">{{ $errors->first('mobile') }}</span> @endif </div> <div class="gst form-group {{ $errors->has('email') ? 'has-error' : '' }}"> <label class="span6 control-label">Email:</label> <input type="email" name="email" class="form-control" value="{{ old('email') }}"> @if ($errors->has('email')) <span class="text-danger">{{ $errors->first('email') }}</span> @endif </div> <div class="gst form-group {{ $errors->has('password') ? 'has-error' : '' }}"> <label class="span6 control-label">Password:</label> <input type="password" name="password" class="form-control"> @if ($errors->has('password')) <span class="text-danger">{{ $errors->first('password') }}</span> @endif </div> <div class="gst form-group {{ $errors->has('repeat_password') ? 'has-error' : '' }}"> <label class="span6 control-label">Confirm Password:</label> <input type="password" name="repeat_password" class="form-control"> @if ($errors->has('repeat_password')) <span class="text-danger">{{ $errors->first('repeat_password') }}</span> @endif </div> <div class="gst form-group"> <button class="btn btn-success" type="submit">Submit</button> </div> </form> </div> </body> </html>
The Laravel exists() function as well as unique(), these two main Laravel rules are used to input fields validate request all the data against check data that is Saved in the mysql database table.
// laravel validation regex exists example 'member_id' => 'required|exists:members,id' // laravel custom validation unique example 'email' => 'required|unique:members,email'
We can also simple use the PHP Based Laravel Rule class to main class define the jquery rule data fluently.
use Illuminate\Validation\Rule; Validator::make($data, [ 'email' => [ 'required', Rule::exists('members')->where(function ($query) { $query->where('status', 1); }), ], ]);
Angular 6 CRUD Operations Application Tutorials
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about PHP Laravel Validation Example Tutorial From Scratch.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donβt forget to share.