Sử dụng bcrypt đăng ký pass, sử dụng get, post cho cùng 1 route, validate form (ok)
https://www.itsolutionstuff.com/post/laravel-9-form-validation-tutorial-exampleexample.html
C:\xampp\htdocs\test\routes\web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FormController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('user/create', [ FormController::class, 'create' ]);
Route::post('user/create', [ FormController::class, 'store' ]);
C:\xampp\htdocs\test\app\Http\Controllers\FormController.php
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class FormController extends Controller {
public function create() {
return view('createUser');
}
public function store(Request $request) {
$validatedData = $request->validate([
'name' => 'required',
'password' => 'required|min:5',
'email' => 'required|email|unique:users',
], [
'name.required' => 'Name field is required.',
'password.required' => 'Password field is required.',
'email.required' => 'Email field is required.',
'email.email' => 'Email field must be email address.',
]);
$validatedData['password'] = bcrypt($validatedData['password']);
$user = User::create($validatedData);
return back()->with('success', 'User created successfully.');
}
}
C:\xampp\htdocs\test\resources\views\createUser.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 9 Form Validation Example - ItSolutionStuff.com</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Laravel 9 Form Validation Example - ItSolutionStuff.com</h1>
@if(Session::has('success'))
<div class="alert alert-success">
{{ Session::get('success') }}
@php Session::forget('success');@endphp
</div>
@endif
<!-- Way 1: Display All Error Messages -->
@if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form method="POST" action="{{ url('user/create') }}">
@csrf
<div class="mb-3">
<label class="form-label" for="inputName">Name:</label>
<input type="text" name="name" id="inputName" class="form-control @error('name') is-invalid @enderror" placeholder="Name">
@error('name')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>
<div class="mb-3">
<label class="form-label" for="inputPassword">Password:</label>
<input type="password" name="password" id="inputPassword" class="form-control @error('password') is-invalid @enderror" placeholder="Password">
@if ($errors->has('password')) <span class="text-danger">{{ $errors->first('password') }}</span> @endif
</div>
<div class="mb-3">
<label class="form-label" for="inputEmail">Email:</label>
<input type="text" name="email" id="inputEmail" class="form-control @error('email') is-invalid @enderror" placeholder="Email">
@error('email')
<span class="text-danger">{{ $message }}</span>
@endif
</div>
<div class="mb-3">
<button class="btn btn-success btn-submit">Submit</button>
</div>
</form>
</div>
</body>
</html>
Previous[COMMON] sử dụng Session::get('success'), Session::get('images') một cách thành thạo (ok)NextBạn đã hiểu rõ lệnh php artisan make:model Post -mcr chưa? (ok)
Last updated