laravel 表單驗證實現(xiàn)多個字段組合后唯一
Laravel 表單驗證器的幾種使用方法
1、使用控制器的 validate 方法進行參數(shù)驗證
/** * 保存一篇新的博客文章。 * * @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)建驗證器實例進行驗證
使用默認的驗證信息
/** * 保存一篇新的博客文章。 * * @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ù)庫 }
使用自定義的驗證信息
/** * 保存一篇新的博客文章。 * * @param Request $request * @return Response */ public function store(Request $request) { $rules = [ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]; $messages = [ 'title.required' => '請?zhí)顚懳恼聵祟}', 'title.unique' => '文章標題不能重復', 'title.max' => '文章標題不能超過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)建表單請求進行驗證
創(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()], ]; } /** * 獲取已定義的驗證規(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 請求 * 驗證失敗,返回錯誤信息 * * @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 表單驗證中,常會遇到需要幾個字段組合起來做唯一限制。
解決方案如下:
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 表單驗證實現(xiàn)多個字段組合后唯一的文章就介紹到這了,更多相關(guān)laravel 表單驗證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
laravel高級的Join語法詳解以及使用Join多個條件
今天小編就為大家分享一篇laravel高級的Join語法詳解以及使用Join多個條件,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10thinkphp解決數(shù)據(jù)傳入數(shù)據(jù)庫中特殊字符的問題小結(jié)
這篇文章主要介紹了thinkphp解決數(shù)據(jù)傳入數(shù)據(jù)庫中特殊字符的問題,為了解決這個問題,你需要確保在插入數(shù)據(jù)庫之前,不對文本內(nèi)容進行HTML實體編碼,需要的朋友可以參考下2024-03-03使用laravel和ajax實現(xiàn)整個頁面無刷新的操作方法
今天小編就為大家分享一篇使用laravel和ajax實現(xiàn)整個頁面無刷新的操作方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10PHP依賴倒置(Dependency Injection)代碼實例
這篇文章主要介紹了PHP依賴倒置(Dependency Injection)代碼實例本文只提供實現(xiàn)代碼,需要的朋友可以參考下2014-10-10