currentScene = $scene; return $this; } /** * 覆盖自动验证方法 */ public function validateResolved() { if ($this->autoValidate) { $this->handleValidate(); } } /** * 验证方法 * @param string $scene * @throws \Illuminate\Auth\Access\AuthorizationException * @throws \Illuminate\Validation\ValidationException */ public function validate($scene = '') { if ($scene) { $this->currentScene = $scene; } $this->handleValidate(); } /** * 根据场景获取规则 * @return array|mixed */ public function getRules() { $rules = $this->container->call([$this, 'rules']); $newRules = []; if ($this->currentScene && isset($this->scenes[$this->currentScene])) { $sceneFields = is_array($this->scenes[$this->currentScene]) ? $this->scenes[$this->currentScene] : explode(',', $this->scenes[$this->currentScene]); foreach ($sceneFields as $field) { if (array_key_exists($field, $rules)) { $newRules[$field] = $rules[$field]; } } return $newRules; } return $rules; } /** * 覆盖设置 自定义验证器 * @param $factory * @return mixed */ public function validator($factory) { return $factory->make( $this->validationData(), $this->getRules(), $this->messages(), $this->attributes() ); } /** * 最终验证方法 * @throws \Illuminate\Auth\Access\AuthorizationException * @throws \Illuminate\Validation\ValidationException */ protected function handleValidate() { $instance = $this->getValidatorInstance(); if ($instance->fails()) { $this->failedValidation($instance); } $this->passedValidation(); } /** * 重写报错部分-适应API JSON下发的需求 */ protected function failedValidation(Validator $validator) { $error= $validator->errors()->all(); throw new HttpResponseException(response()->json(['status' => false, 'code' => 422, 'message' => $error[0], 'data' => []])); } }