Các rule validation có sẵn (Available validation rules)
Accepted
Trường được xác thực phải có giá trị là "yes", "on", 1 hoặc true. Hữu dụng khi tạo checkbox "Ghi nhớ đăng nhập" hoặc "Đồng ý điều khoản và dịch vụ"...
C:\xampp8\htdocs\plugindev\routes\web.php
<?phpuseApp\Http\Controllers\PostController;useIlluminate\Support\Facades\Route;/*|--------------------------------------------------------------------------| 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 () {returnview('welcome');});Route::get('post/create', [PostController::class,'create']);Route::post('/post', [PostController::class,'store']);
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Support\Facades\Validator;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $validator =Validator::make($request->all(), ['remember_me'=>'accepted' ]);if ($validator->fails()) {returnresponse()->json($validator->errors(),422); }dd('Done'); }}
Trường được xác thực phải có bản ghi A hoặc AAAA hợp lệ theo dns_get_record của PHP. Cái này theo mình biết thường dùng để kiểm tra tên miền đã được đăng ký hay chưa, các bạn có thể Google để tìm hiểu thêm. Thường áp dụng cho mấy website thuê tên miền, hosting...
C:\xampp8\htdocs\plugindev\routes\web.php
<?phpuseApp\Http\Controllers\PostController;useIlluminate\Support\Facades\Route;/*|--------------------------------------------------------------------------| 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 () {returnview('welcome');});Route::get('post/create', [PostController::class,'create'])->name('get');Route::post('/post', [PostController::class,'store'])->name('post');
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Support\Facades\Validator;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $request->validate(['site_active_url'=>'required|active_url' ]);dd('Done'); }}
Trường được xác thực phải có giá trị là một date hợp lệ, không liên quan theo hàm strtotime của PHP.
C:\xampp8\htdocs\plugindev\routes\web.php
<?phpuseApp\Http\Controllers\PostController;useIlluminate\Support\Facades\Route;/*|--------------------------------------------------------------------------| 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 () {returnview('welcome');});Route::get('post/create', [PostController::class,'create'])->name('get');Route::post('/post', [PostController::class,'store'])->name('post');
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Support\Facades\Validator;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $request->validate(['date_string'=>'date' ]);dd('Done'); }}
Trường được xác thực phải phù hợp với định dạng date đã cho. Bạn chỉ nên sử dụng một trong hai rule date hoặc date_format để xác định một trường, không phải cả hai.
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Support\Facades\Validator;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $validator = $request->validate(['date_string'=>'date_format:"d/m/Y"' ]);dd('Done'); }}
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Support\Facades\Validator;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $request->validate(['date_string'=>'date_equals:date' ]);dd('Done'); }}
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Support\Facades\Validator;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $validator = $request->validate(['date_string'=>'date_format:"d/m/Y"|after:"10/06/2001"' ]);dd('Done'); }}
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Support\Facades\Validator;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $validator = $request->validate(['date_string'=>'date_format:"d/m/Y"|after_or_equal:"10/06/2001"' ]);dd('Done'); }}
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Support\Facades\Validator;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $validator = $request->validate(['title'=>'alpha' ]);dd('Done'); }}
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Support\Facades\Validator;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $validator = $request->validate(['title'=>'alpha_dash' ]);dd('Done'); }}
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Support\Facades\Validator;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $validator = $request->validate(['title'=>'alpha_num' ]);dd('Done'); }}
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Support\Facades\Validator;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $request->validate(['text'=>'numeric' ]);dd("done1"); }}
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Support\Facades\Validator;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $request->validate(['numeric'=>'digits:3' ]);dd('Done'); }}
Digits between
Trường được xác thực phải là số và có độ dài nằm trong khoảng min, max đã cho.
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Support\Facades\Validator;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $request->validate(['numeric'=>'digits_between:3,9' ]);dd('Done'); }}
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Support\Facades\Validator;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $validator = $request->validate(["country_name"=>"required|array","country_name.*"=>"required", ]);dd('Done'); }}
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Support\Facades\Validator;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $array = [ ['id'=>1], ['id'=>1], ]; $rules = ['*.id'=>'distinct', ];Validator::make($array, $rules)->validate();dd('done'); }}
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Support\Facades\Validator;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $array = ['test'=> [ ['id'=>1], ['id'=>1] ] ]; $rules = ['test.*.id'=>'distinct', ];Validator::make($array, $rules)->validate();dd('done'); }}
In
Trường được xác thực phải là một phần tử nằm trong mảng đã cho.
Mặc định:
'item' => 'in:' . implode(',', [1, 2, 3, 4, 5])
Đôi khi sử dụng valiation rule còn khá nhiều hạn chế. Chính vì vậy, ta có thể sử dụng method Rule::in để linh hoạt hơn.
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $request->validate(['item'=>'in:'.implode(',', [1,2,3,4,5]) ]);dd('Done'); }}
C:\xampp8\htdocs\plugindev\routes\web.php
<?phpuseApp\Http\Controllers\PostController;useIlluminate\Support\Facades\Route;/*|--------------------------------------------------------------------------| 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 () {returnview('welcome');});Route::get('post/create', [PostController::class,'create'])->name('get');Route::post('/post', [PostController::class,'store'])->name('post');
Trường được xác thực phải có giá trị thuộc các key của mảng. Thường để kiểm tra một giá trị có tồn tại trong multi-level array hay không.
C:\xampp8\htdocs\plugindev\routes\web.php
<?phpuseApp\Http\Controllers\PostController;useIlluminate\Support\Facades\Route;/*|--------------------------------------------------------------------------| 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 () {returnview('welcome');});Route::get('post/create', [PostController::class,'create'])->name('get');Route::post('/post', [PostController::class,'store'])->name('post');
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $request->merge(['users'=> [ ['id'=>1,'username'=>'lechihuy' ], ['id'=>2,'username'=>'huydeo' ] ] ]); $request->validate(['id'=>'in_array:users.*' ]);dd('Done'); }}
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Support\Facades\Validator;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $validator = $request->validate(['date_string'=>'before:date' ]);dd('Done'); }}
<?phpnamespaceApp\Http\Controllers;useIlluminate\Http\Request;useIlluminate\Support\Facades\Validator;classPostControllerextendsController{/** * Display a listing of the resource. * * @return\Illuminate\Http\Response */publicfunctionindex() {// }/** * Show the form for creating a new resource. * * @return\Illuminate\Http\Response */publicfunctioncreate() {returnview('posts.create'); }/** * Store a newly created resource in storage. * * @param\Illuminate\Http\Request $request * @return\Illuminate\Http\Response */publicfunctionstore(Request $request) { $validator = $request->validate(['answer'=>'required|boolean' ]);dd('Done'); }}
Trường foo được xác thực phải khớp với trường foo_confirmation. Chẳng hạn bạn muốn xác thực trường password với rule confirmed, trường password_confirmation phù hợp (khớp với trường password) phải có mặt trong request.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validator = $request->validate([
'password' => 'confirmed'
]);
dd('Done');
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'input1' => 'different:input2'
]);
dd('Done');
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
// Ensures to the image width and height
$this->validate($request, [
'avatar' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048|dimensions:width=500,height=500',
]);
dd('done');
}
}
Trường được xác thực phải là một địa chỉ email. Trong giới hạn, rule này sử dụng gói egulias/email-vaidator để validate email. Trong đó có các validator như:
rfc: RFCValidator
strict: NoRFCWarningsValidation
spoof: SpoofCheckValidation
dns: DNSCheckValidation
filter: FilterEmailValidation
Mặc định Laravel sử dụng validator rfc để validate. Với validator filter, nó sử dụng method filter_var để validate.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'email' => 'email'
]);
dd('Done');
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'text' => 'ends_with:foo,bar'
]);
dd('Done');
}
}
Exists (Database)
Trường được xác thực phải chứa giá trị tồn tại trong cột của database table. Thường dùng để kiểm tra xem ID có tồn tại trước khi thực hiện sửa hoặc xóa...
Nếu column không được chỉ định thì sẽ tự động lấy tên của trường đển gán cho column.
'id' => 'exists:users'
Trường hợp khai báo column:
'id' => 'exists:users,id'
Nếu ứng dụng của bạn sử dụng nhiều database connection, có thể sử dụng ký hiệu . để tham chiếu theo ý muốn.
'id' => 'exists:mysql.users,id'
Mặc định thì rule này sẽ sử dụng câu truy vấn:
select count(*) as aggregate from `connect.table` where `column` = value
Nếu bạn muốn thay đổi thiết lập này, bạn có thể sử dụng method Rule::exists:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\File;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'file' => 'file'
]);
$fileModel = new File;
if ($request->file()) {
$fileName = time() . '_' . $request->file->getClientOriginalName();
$filePath = $request->file('file')->storeAs('uploads', $fileName, 'public');
$fileModel->name = time() . '_' . $request->file->getClientOriginalName();
$fileModel->file_path = '/storage/' . $filePath;
$fileModel->save();
return back()
->with('success', 'File has been uploaded.')
->with('file', $fileName);
}
dd('Done');
}
}
C:\xampp8\htdocs\plugindev\app\Models\File.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class File extends Model
{
use HasFactory;
protected $fillable = [
'name',
'file_path',
];
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('files', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
$table->string('file_path')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('files');
}
};
Trường được xác thực phải lớn hơn trường đã cho. Hai trường phải cùng loại dữ liệu, bao gồm: chuỗi, số, mảng và file. Về quy ước so sánh tương tự như rule between.
'gt' => [
'numeric' => 'The :attribute must be greater than :value.',
'file' => 'The :attribute must be greater than :value kilobytes.',
'string' => 'The :attribute must be greater than :value characters.',
'array' => 'The :attribute must have more than :value items.',
]
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'text_1' => 'gt:text_2'
]);
dd('Done');
}
}
'gte' => [
'numeric' => 'The :attribute must be greater than or equal :value.',
'file' => 'The :attribute must be greater than or equal :value kilobytes.',
'string' => 'The :attribute must be greater than or equal :value characters.',
'array' => 'The :attribute must have :value items or more.',
]
Trường được xác thực phải lớn hơn hoặc bằng trường đã cho. Hai trường phải cùng loại dữ liệu, bao gồm: chuỗi, số, mảng và file. Về quy ước so sánh tương tự như rule between.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'text_1' => 'gte:text_2'
]);
dd('Done');
}
}
Trường được xác thực phải nhỏ hơn trường đã cho. Hai trường phải cùng loại dữ liệu, bao gồm: chuỗi, số, mảng và file. Về quy ước so sánh tương tự như rule
'lt' => [
'numeric' => 'The :attribute must be less than :value.',
'file' => 'The :attribute must be less than :value kilobytes.',
'string' => 'The :attribute must be less than :value characters.',
'array' => 'The :attribute must have less than :value items.',
]
Less than or equal
Trường được xác thực phải nhỏ hơn hoặc bằng trường đã cho. Hai trường phải cùng loại dữ liệu, bao gồm: chuỗi, số, mảng và file. Về quy ước so sánh tương tự như rule between.
'lte' => [
'numeric' => 'The :attribute must be less than or equal :value.',
'file' => 'The :attribute must be less than or equal :value kilobytes.',
'string' => 'The :attribute must be less than or equal :value characters.',
'array' => 'The :attribute must not have more than :value items.',
],
Min
rường được xác thực phải lớn hơn hoặc bằng giá trị lớn nhất cho trước, bao gồm: chuỗi, số, mảng và file. Về quy ước so sánh tương tự như rule between.
'min' => [
'numeric' => 'The :attribute may not be greater than :max.',
'file' => 'The :attribute may not be greater than :max kilobytes.',
'string' => 'The :attribute may not be greater than :max characters.',
'array' => 'The :attribute may not have more than :max items.',
],
Max
Trường được xác thực phải nhỏ hơn hoặc bằng giá trị lớn nhất cho trước, bao gồm: chuỗi, số, mảng và file. Về quy ước so sánh tương tự như rule between.
'max' => [
'numeric' => 'The :attribute may not be greater than :max.',
'file' => 'The :attribute may not be greater than :max kilobytes.',
'string' => 'The :attribute may not be greater than :max characters.',
'array' => 'The :attribute may not have more than :max items.',
],
Image
Trường được xác thực phải là tệp ảnh, bao gồm các định dạng: jpeg, png, bmp, gif, svg, hoặc webp.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'avatar' => 'image'
]);
dd('Done');
}
}
C:\xampp8\htdocs\plugindev\routes\web.php
<?php
use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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('post/create', [PostController::class, 'create'])->name('get');
Route::post('/post', [PostController::class, 'store'])->name('post');
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'ip' => 'ip'
]);
dd('Done');
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'string_json' => 'json'
]);
dd('Done');
}
}
MIME types (Nó không hoạt động 1 mình mà kết hợp với Extension (File)
File được xác thực phải khớp với một trong các MIME đã cho. Để xác thực được MIME của file, nội dung file sẽ được đọc và framework sẽ dự đoán loại MIME, điều này có thể khác với loại MIME cho người dùng cung cấp.
video' => 'mimetypes:video/avi,video/mpeg'
Extension (File)
File được xác thực phải khớp với một trong các định dạng mở rộng đã cho. Rule này sẽ xác thực MIME trước thông qua đọc nội dụng file và đoán rồi mới xác thực định dạng mở rộng. Có thể nói đây là một rule mở rộng của mimetypes. Bạn có thể tham khảo một số các MIME tại
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'video' => 'mimes:mp4'
]);
dd("done1");
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'video' => 'mimes:mp4|mimetypes:video/mp4'
]);
dd("done1");
}
}
Regex
Trường được xác thực phải khớp với regular expression đã cho. Rule này bản chất sử dụng hàm preg_match trong PHP, do đó cần chú ý đến các ký tự của chuỗi pattern.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'text' => 'regex:/^[a-z]+$/'
]);
dd("done1");
}
}
Trường được xác thực không được khớp với regular expression đã cho. Rule này bản chất sử dụng hàm preg_match trong PHP, do đó cần chú ý đến các ký tự của chuỗi pattern.
C:\xampp8\htdocs\plugindev\routes\web.php
<?php
use App\Http\Controllers\PostController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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('post/create', [PostController::class, 'create'])->name('get');
Route::post('/post', [PostController::class, 'store'])->name('post');
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'text' => 'not_regex:/^[a-z]+$/'
]);
dd("done1");
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'text' => 'nullable|alpha'
]);
dd("done1");
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name' => 'present',
'short_code' => 'required'
]);
dd("done1");
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'text' => 'required'
]);
dd("done1");
}
}
Trường được xác thực phải có mặt trong request và không được trống nếu như một trường khác có giá trị bằng giá trị đã cho.
Ngoài ra bạn cũng có thể sử dụng method Rule::requiredIf để linh hoạt hơn. Method này chấp nhận giá trị boolean hoặc một Closure object trả về gía trị boolean. Nếu là true thì validation rule này sẽ được thực thi.
'field' => Rule::requiredIf(boolean)
'field' => Rule::required(function () use ($request) {
return boolean;
});
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$input = $request->all();
$request->validate([
'field' => 'required',
'field2' => 'required_unless:field,1,2,3',
]);
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$input = $request->all();
$request->validate([
'field' => 'required',
'anotherfield' => 'required_with:field'
]);
dd('done');
}
}