Laravel 表单验证器的几种使用方法
1、使用控制器的 validate 方法进行参数验证
/** * 保存一篇新的博客文章。 * * @param Request $request * @return Response */ public function store(Request $request) { $this->validate($request, [ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // 文章内容是符合规则的,存入数据库 }
2、手动创建验证器实例进行验证
使用默认的验证信息
/** * 保存一篇新的博客文章。 * * @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(); } // 文章内容是符合规则的,存入数据库 }
使用自定义的验证信息
/** * 保存一篇新的博客文章。 * * @param Request $request * @return Response */ public function store(Request $request) { $rules = [ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]; $messages = [ 'title.required' => '请填写文章标题', 'title.unique' => '文章标题不能重复', 'title.max' => '文章标题不能超过255个字符', 'body.required' => '请填写文章内容', ]; $validator = Validator::make($request->all(), $rules, $messages); if ($validator->fails()) { return redirect('post/create')->withErrors($validator)->withInput(); } // 文章内容是符合规则的,存入数据库 }
3、创建表单请求进行验证
创建表单请求文件:php artisan make:request ExampleRequest
表单请求文件内容:
<?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()], ]; } /** * 获取已定义的验证规则的错误消息。 * * @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'] ];
到此这篇关于laravel 表单验证实现多个字段组合后唯一的文章就介绍到这了,更多相关laravel 表单验证内容请搜索NICE源码以前的文章或继续浏览下面的相关文章希望大家以后多多支持NICE源码!