laravel 表單驗(yàn)證實(shí)現(xiàn)多個字段組合后唯一
Laravel 表單驗(yàn)證器的幾種使用方法
1、使用控制器的 validate 方法進(jìn)行參數(shù)驗(yàn)證
/**
* 保存一篇新的博客文章。
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
// 文章內(nèi)容是符合規(guī)則的,存入數(shù)據(jù)庫
}
2、手動創(chuàng)建驗(yàn)證器實(shí)例進(jìn)行驗(yàn)證
使用默認(rèn)的驗(yàn)證信息
/**
* 保存一篇新的博客文章。
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$rules = [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return redirect('post/create')->withErrors($validator)->withInput();
}
// 文章內(nèi)容是符合規(guī)則的,存入數(shù)據(jù)庫
}
使用自定義的驗(yàn)證信息
/**
* 保存一篇新的博客文章。
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$rules = [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
];
$messages = [
'title.required' => '請?zhí)顚懳恼聵?biāo)題',
'title.unique' => '文章標(biāo)題不能重復(fù)',
'title.max' => '文章標(biāo)題不能超過255個字符',
'body.required' => '請?zhí)顚懳恼聝?nèi)容',
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return redirect('post/create')->withErrors($validator)->withInput();
}
// 文章內(nèi)容是符合規(guī)則的,存入數(shù)據(jù)庫
}
3、創(chuàng)建表單請求進(jìn)行驗(yàn)證
創(chuàng)建表單請求文件:php artisan make:request ExampleRequest
表單請求文件內(nèi)容:
<?php
namespace App\Http\Requests;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
class ExampleRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required|max:20',
'name' => ['required', new Uppercase()],
];
}
/**
* 獲取已定義的驗(yàn)證規(guī)則的錯誤消息。
*
* @return array
*/
public function messages()
{
return [
'title.required' => 'A title is required',
'title.max' => 'The title may not be greater than 20 characters.',
];
}
/**
* 兼容 form 表單請求與 ajax 請求或者 json api 請求
* 驗(yàn)證失敗,返回錯誤信息
*
* @param Validator $validator
* @throws
*/
protected function failedValidation(Validator $validator)
{
if ($this->wantsJson() || $this->ajax()) {
throw new HttpResponseException(
new JsonResponse([
'code' => 500,
'msg' => $validator->errors()->first(),
'data' => new \stdClass()
])
);
} else {
parent::failedValidation($validator);
}
}
}
在控制器中使用 ExampleRequest
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Http\Requests\ExampleRequest;
class ExampleController extends Controller
{
public function valid(ExampleRequest $request)
{
$params = $request->all();
dd($params);
}
}
在laravel 表單驗(yàn)證中,常會遇到需要幾個字段組合起來做唯一限制。
解決方案如下:
where[] = ['parentId','=',where[]=[′parentId ′,′ = ′,this->request->get('parentId')];
return [
'menuTitle' => ['required', 'max:32','min:2',Rule::unique('admin_menu','menuTitle')->where(function($query)use($where){
$query->where($where)->whereNull('deleted_at');
})->ignore($id) ],
'menuTitleEn' => ['required', 'max:32','min:2',Rule::unique('admin_menu','menuTitleEn')->where(function($query)use($where){
$query->where($where)->whereNull('deleted_at');
})->ignore($id) ],
'menuRoute' => ['required',Rule::unique('admin_menu','menuRoute')->ignore($id)],
'menuIcon' => ['required', 'min:2','max:32'],
'routeName' => ['sometimes', 'min:2','max:32'],
'parentId' => ['required','numeric'],
'order'=>['sometimes','numeric']
];
到此這篇關(guān)于laravel 表單驗(yàn)證實(shí)現(xiàn)多個字段組合后唯一的文章就介紹到這了,更多相關(guān)laravel 表單驗(yàn)證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
php 數(shù)組處理函數(shù)extract詳解及實(shí)例代碼
php extract 函數(shù)使用數(shù)組鍵名作為變量名,使用數(shù)組鍵值作為變量值, 本函數(shù)可以處理表單提交并插入數(shù)據(jù)庫。文章向大家講解extract函數(shù)的基本使用方法及實(shí)例,需要的朋友可以參考下2016-11-11
PHP+FFMPEG實(shí)現(xiàn)將視頻自動轉(zhuǎn)碼成H264標(biāo)準(zhǔn)Mp4文件
最近做一個在線教學(xué)網(wǎng)的項(xiàng)目,需要實(shí)現(xiàn)上傳任意格式視頻自動為h264標(biāo)準(zhǔn)視頻,使用html5播放。最終使用PHP+FFMPEG實(shí)現(xiàn),在此將詳細(xì)解決方案分享給大家!2014-09-09
PHP笛卡爾積實(shí)現(xiàn)原理及代碼實(shí)例
這篇文章主要介紹了PHP笛卡爾積實(shí)現(xiàn)原理及代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-12-12

