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)文章
laravel配置Redis多個庫的實(shí)現(xiàn)方法
這篇文章主要介紹了laravel配置Redis多個庫的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04laravel高級的Join語法詳解以及使用Join多個條件
今天小編就為大家分享一篇laravel高級的Join語法詳解以及使用Join多個條件,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10

thinkphp解決數(shù)據(jù)傳入數(shù)據(jù)庫中特殊字符的問題小結(jié)

使用laravel和ajax實(shí)現(xiàn)整個頁面無刷新的操作方法

PHP依賴倒置(Dependency Injection)代碼實(shí)例

分享PHP-pcntl 實(shí)現(xiàn)多進(jìn)程代碼