相關系列文章:
1.Laravel 12 表單驗證教學|用正確方式驗證使用者輸入
在\resources\views\students\add.blade.php 去除 required
在\resources\views\students\add.blade.php 去除 required
@extends('layouts.app')
@section('head')
<title>Add Students</title>
@endsection
@section('content')
<section>
<div class="card shadow-lg">
<div class="card-header bg-primary text-white">
<h5 class="mb-0">Student Registration</h5>
</div>
<div class="card-body">
<form action="{{ URL('student/create') }}" method="POST">
@csrf
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name" name="name">
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email">
</div>
<div class="mb-3">
<label for="age" class="form-label">Age</label>
<input type="number" class="form-control" id="age" name="age">
</div>
<div class="mb-3">
<label for="age" class="form-label">Date of Birth</label>
<input type="date" class="form-control" id="date_of_birth" name="date_of_birth">
</div>
<div class="mb-3">
<label class="form-label">Gender</label>
<div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="gender" id="male" value="m" >
<label class="form-check-label" for="male">Male</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="gender" id="female" value="f">
<label class="form-check-label" for="female">Female</label>
</div>
</div>
</div>
<button type="submit" class="btn btn-success">送出</button>
</form>
</div>
</div>
</section>
@endsection
在http://127.0.0.1:8000/student/add,什麼資料都不填,直接按下[送出]
就會看到
在\app\Http\Controllers\StudentController.php 編輯
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|unique:students,email',
'age' => 'required|integer|min:1|max:100',
'date_of_birth' => 'required|date',
'gender' => 'required|in:m,f',
]);
如下圖:
2.Laravel 12 的 Form Request 驗證|乾淨且組織化的驗證方式
資料來源:
1.Laravel 12 Form Validation Tutorial | Validate User Input the Right Way
2.Laravel 12 Form Request Validation | Clean & Organized Validation in Laravel



沒有留言:
張貼留言