/** * 保存一篇新的博客文章。 * * @param Request $request * @return Response */ public function store(Request $request) { $this->validate($request, [ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); // 文章内容是符合规则的,存入数据库 }
使用默认的验证信息
/** * 保存一篇新的博客文章。 * * @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(); } // 文章内容是符合规则的,存入数据库 }
创建表单请求文件: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); } }
解决方案如下:
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 表单验证内容请搜索鸟哥教程(niaoge.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持鸟哥教程(niaoge.com)!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#niaoge.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。