无涯教程: Laravel 8 - 表格验证介绍
本文详细介绍了Laravel 8表格验证教程。我们将查看Laravel 8表单验证的示例和错误消息。
您还可以在laravel 8表单验证中定义自定义错误消息。我们将在每个字段中显示错误消息。我们将使用has()来检查laravel 8中的错误消息。
在这里,我将向您展示表单验证的非常简单的示例,您可以简单地在您的Laravel 8项目中使用。
创建路由
在这里,我们正在学习Laravel 8中的简单且简单的验证示例,只需在web.php文件中的路由中添加。
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
Route::get('user/create', [ HomeController::class, 'create' ]);
Route::post('user/create', [ HomeController::class, 'store' ]);
创建控制器
现在我们将添加两个控制器方法,一个只会显示带有get请求的blade视图文件,另一个用于发布请求。
app/Http/Controller/HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class HomeController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('createUser');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validatedData = $request->validate([
'name' => 'required',
'password' => 'required|min:5',
'email' => 'required|email|unique:users'
], [
'name.required' => 'Name is required',
'password.required' => 'Password is required'
]);
$validatedData['password'] = bcrypt($validatedData['password']);
$user = User::create($validatedData);
return back()->with('success', 'User created successfully.');
}
}
创建视图文件
现在我们将创建createUser.blade.php文件,在这里,我们将创建具有错误验证消息的引导简单表单。所以,让我们创建以下文件:
resources/views/createUser.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 8 form validation example - 无涯教程</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="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Laravel 8 Form Validation Example - 无涯教程</h1>
@if(Session::has('success'))
<div class="alert alert-success">
{{ Session::get('success') }}
@php
Session::forget('success');
@endphp
</div>
@endif
<form method="POST" action="{{ url('user/create') }}">
{{ csrf_field() }}
<div class="form-group">
<label>Name:</label>
<input type="text" name="name" class="form-control" placeholder="Name">
@if ($errors->has('name'))
<span class="text-danger">{{ $errors->first('name') }}</span>
@endif
</div>
<div class="form-group">
<label>Password:</label>
<input type="password" name="password" class="form-control" placeholder="Password">
@if ($errors->has('password'))
<span class="text-danger">{{ $errors->first('password') }}</span>
@endif
</div>
<div class="form-group">
<strong>Email:</strong>
<input type="text" name="email" class="form-control" placeholder="Email">
@if ($errors->has('email'))
<span class="text-danger">{{ $errors->first('email') }}</span>
@endif
</div>
<div class="form-group">
<button class="btn btn-success btn-submit">Submit</button>
</div>
</form>
</div>
</body>
</html>
来源:https://www.learnfk.com/article-laravel-8-form-validation-exampleexample
Hi LearnFk.com