laravel的验证可以直接在控制器里面这样验证:
$validatedData = $this->validate([ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ]); if ($validatedData ->fails()) { //未通过处理 }
或
Validator::make($request->all(), [ 'title' => 'required|unique:posts|max:255', 'body' => 'required', ])->validate();
但在控制器里面直接验证,没有做到分离。这样我们可以把表单验证放在一个Request类里,实现高度可复用,下面是具体实现:
<?php namespace App\Http\Requests; use Illuminate\Contracts\Validation\Validator; use Illuminate\Validation\ValidationException; class YarnSalesOrderRequest extends Request { protected function failedValidation(Validator $validator) { throw new ValidationException($validator, $this->response( $this->formatErrors($validator))); } /** * 重写错误提示方法 * @param Validator $validator * @return array */ protected function formatErrors(Validator $validator) { $message = $validator->errors()->all(); $result = [ 'message' => $message[0],//这里只需要返回第一个提示信息 ]; return $result; } /** * 验证规则,需要验证的字段 * @var array */ protected $_rules = [ 'type' => 'required|integer|regex:/^[1-9][0-9]*$/',//正则验证正整数 'contact' => 'required', 'supplier_id' => 'required|integer|regex:/^[1-9][0-9]*$/', 'version' => 'required|integer|min:1', 'sys_fabric_company_id' => 'same:supplier_id' ]; /** * 字段属性名称 * @var array */ protected $_attributes = [ 'type' => '订单类型', 'customer_id' => '客户', 'contact' => '联系方式', 'supplier_id' => '供应商', 'sys_fabric_company_id' => '公司', 'seq' => '序号', 'quantity' => '数量', 'unit_id' => '单位', 'unit_price' => '单价(不含税)', 'money' => '金额(不含税)', ]; /** * 提示信息 * @var array */ protected $_messages = [ 'required' => '为必填项', 'min' => '最小为:min', 'max' => '最大为:max', 'between' => '长度在:min和:max之间', 'integer' => '不能为空', 'same' => '必须与供应商一致', 'numeric' => '必须为数值', 'regex' => '不能为空或不符合规则', ]; /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true;//默认值false修改为true } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { $rules = $this->_rules; //处理分录字段 foreach ($this->request->get('details') as $key => $value) { $rules['details.' . $key . '.seq'] = 'integer'; $rules['details.' . $key . '.quantity'] = 'required|numeric|regex:/^\d+(\.\d+)?$/'; $rules['details.' . $key . '.unit_id'] = 'integer|regex:/^[1-9][0-9]*$/'; $rules['details.' . $key . '.unit_price'] = 'numeric|regex:/^\d+(\.\d+)?$/'; $rules['details.' . $key . '.money'] = 'numeric|regex:/^\d+(\.\d+)?$/'; } return $rules; } /** * 返回错误信息 * @return array|mixed */ public function messages() { $_rules = $this->rules(); $attributes = $this->_attributes; $messages = $this->_messages; $data = []; foreach ($_rules as $key => $rule) { $new_rules = explode('|', $rule);//分割成数组 $rule_key = $key; if (str_contains($rule_key, '.')) { $rule_key = substr(strrchr($rule_key, '.'), 1); } foreach ($new_rules as $new_rule) { $head = strstr($new_rule, ':', true);//截取:之前的字符串 if ($head) { $new_rule = $head; } $data[$key . '.' . $new_rule] = $attributes[$rule_key] . $messages[$new_rule]; } } return $data; } }