Laravel Form Validation Example
Laravel is the powerful framework for developing web application. Today we talk about form submit validation what is validation. The validation of form is help the user to alert the specific field or input. Before post a form the developer create form validation function for catch the error or input the correct data.
In Laravel Framework there are lot of validation but in this tutorial we discuss the basic form submit validation for example catch the black input field and character limit.
Step 01: Create a view user login.blade.php and past the following code.
@include('inner')
<h1 style="text-align: center;">User Login Page</h1>
<div class="form-group">
<form action="user" method="post">
@csrf
<input type="text" class="form-control" name="username" placeholder="Enter Usernme"><br>
<span style="color: red;">@error ('username'){{$message}} @enderror</span>
<input type="password" class="form-control" name="password" placeholder="Enter Password"><br>
<span style="color: red;">@error ('password'){{$message}} @enderror</span>
<br><br>
<button class="btn btn-primary" type="submit" name="submit">Submit</button>
</form>
</div>
Note: In the above code I include the inner file for the bootstrap style. So go the the Bootstrap and copy the cdn and bootsrtap css file and pase in the inner.blade.php
Step 02: Create new controller in the app folder with the name of usercontroller and create the function
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
//
function getDate(request $req){
//return "Hellow from controller";
$req->validate([
'username'=>'required | max:10',
'password'=>'required | min:6'
]);
return $req->input();
}
}
Step 03: Use the usercontroll in the web.php file on the namespace
use App\Http\Controllers\UserController;
Step 04: Create the Rout in the web.php file
Route::post("user",[UserController::class, 'getDate']);
Route::view("user",'user');
Step 05: Run the project
php artisan serve
Pingback: How To Create Middleware In Laravel ? With Easy Example | GSS TECHNOLOGY
Pingback: Laravel 8 Login Session Example | GSS TECHNOLOGY