Laravel 验证Requests
trait SceneValidator
<?php namespace App\Http\Requests; use Illuminate\Contracts\Validation\{Factory, Validator}; trait SceneValidator { protected $scene = null; protected $onlyRule = []; protected $autoValidate = true; /** * Validate. * * @param string|array $scene */ public function validate($scene = '') { if (!$this->autoValidate) { if (is_array($scene)) { $this->onlyRule = $scene; } else { $this->scene = $scene; } $this->handleValidate(); } } /** * 覆盖 ValidatesWhenResolvedTrait->validateResolved */ public function validateResolved() { if (method_exists($this, 'autoValidate')) { $this->autoValidate = $this->container->call([$this, 'autoValidate']); } if ($this->autoValidate) { $this->handleValidate(); } } /** * Handle validate. */ protected function handleValidate() { parent::validateResolved(); } /** * 定义 FormRequest->getValidatorInstance 下 validator 验证器 * * @param Factory $factory * * @return Validator */ public function validator(Factory $factory) { $validationData = $this->isMethod('GET') ? $this->query() : $this->post(); return $factory->make($validationData, $this->getRules(), $this->messages(), $this->attributes()); } /** * Get rules. * * @return array */ protected function getRules() { return $this->handleScene($this->container->call([$this, 'rules'])); } /** * Handle scene. * * @param array $rules * * @return array */ protected function handleScene(array $rules) { if ($this->onlyRule) { return $this->handleRules($this->onlyRule, $rules); } if (!empty($this->scene) && method_exists($this, 'scene')) { $scene = $this->container->call([$this, 'scene']); if (array_key_exists($this->scene, $scene)) { return $this->handleRules($scene[$this->scene], $rules); } } return $rules; } /** * Handle rules. * * @param array $sceneRules * @param array $rules * * @return array */ protected function handleRules(array $sceneRules, array $rules) { $result = []; foreach ($sceneRules as $key => $value) { if (is_numeric($key) && array_key_exists($value, $rules)) { $result[$value] = $rules[$value]; } else { $result[$key] = $value; } } return $result; } }
BaseRequests
<?php namespace App\Http\Requests; use App\Lib\Helper; use Illuminate\Contracts\Validation\Validator; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Http\Exceptions\HttpResponseException; class BaseRequests extends FormRequest { /** * validate验证失败模板 * @param Validator $validator */ protected function failedValidation(Validator $validator) { $message = $validator->getMessageBag()->first(); throw (new HttpResponseException(Helper::appError($message,405))); } }
CommonRequests
<?php namespace App\Http\Requests; class CommonRequests extends BaseRequests { use SceneValidator; /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } }
使用:
AssetCardRequest
<?php namespace App\Http\Requests\Asset; use App\Http\Requests\CommonRequests; class AssetCardRequest extends CommonRequests { protected $autoValidate = false; public function rules() { $mobile = request('id') ? 'required|mobile|unique:asset_cards,mobile,' . request('id') : 'required|mobile|unique:asset_cards,mobile'; return [ 'id' => 'required', 'device_id' => 'nullable', 'mobile' => $mobile, 'operator' => 'required', 'type' => 'required|in:0,1,2,3', 'fee' => 'required', 'outbound_minute' => 'required', 'flow' => 'required', 'account_holder' => 'required', 'real_name' => 'required', // 'real_id_no' => [ // 'required', // 'regex:/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/' // ], 'real_mobile' => 'required|mobile', 'status_id' => 'required', 'remark' => 'nullable', ]; } public function scene() { return [ 'add' => [ 'device_id', 'mobile', 'operator', 'type', 'fee', 'outbound_minute', 'flow', 'account_holder', 'real_name', // 'real_id_no', 'real_mobile', 'status_id', 'remark' ], 'edit' => [ 'id', 'device_id', 'mobile', 'operator', 'type', 'fee', 'outbound_minute', 'flow', 'account_holder', 'real_name', // 'real_id_no', 'real_mobile', 'status_id', 'remark' ] ]; } public function messages() { return [ 'id.required' => '请选择数据', 'device_id.required' => '请选择使用设备', 'mobile.required' => '请填写手机号码', 'mobile.mobile' => '手机号码格式不正确', 'mobile.unique' => '手机号码已存在', 'operator.required' => '请填写运营商', 'type.required' => '请选择卡种类', 'fee.required' => '请填写卡套餐/月', 'outbound_minute.required' => '请填写外呼分钟数', 'flow.required' => '请填写月流量(G)', 'account_holder.required' => '请填写开户人', 'real_name.required' => '请填写实名姓名', 'real_id_no.required' => '请填写实名身份证号码', 'real_id_no.regex' => '实名身份证号码格式不正确', 'real_mobile.required' => '请填写实名手机号码', 'real_mobile.mobile' => '实名手机号码格式错误', 'status_id.required' => '请选择卡状态' ]; } }
控制器: