container->make(ValidationFactory::class); if (method_exists($this, 'validator')) { return $this->container->call([$this, 'validator'], compact('factory')); } return $factory->make( $this->all(), $this->container->call([$this, 'rules']), $this->messages(), $this->attributes() ); } /** * Handle a failed validation attempt. * * @param \Illuminate\Contracts\Validation\Validator $validator * @return mixed */ protected function failedValidation(Validator $validator) { throw new HttpResponseException($this->response( $this->formatErrors($validator) )); } /** * Determine if the request passes the authorization check. * * @return bool */ protected function passesAuthorization() { if (method_exists($this, 'authorize')) { return $this->container->call([$this, 'authorize']); } return false; } /** * Handle a failed authorization attempt. * * @return mixed */ protected function failedAuthorization() { throw new HttpResponseException($this->forbiddenResponse()); } /** * Get the proper failed validation response for the request. * * @param array $errors * @return \Symfony\Component\HttpFoundation\Response */ public function response(array $errors) { if ($this->ajax() || $this->wantsJson()) { return new JsonResponse($errors, 422); } return $this->redirector->to($this->getRedirectUrl()) ->withInput($this->except($this->dontFlash)) ->withErrors($errors, $this->errorBag); } /** * Get the response for a forbidden operation. * * @return \Illuminate\Http\Response */ public function forbiddenResponse() { return new Response('Forbidden', 403); } /** * Format the errors from the given Validator instance. * * @param \Illuminate\Contracts\Validation\Validator $validator * @return array */ protected function formatErrors(Validator $validator) { return $validator->getMessageBag()->toArray(); } /** * Get the URL to redirect to on a validation error. * * @return string */ protected function getRedirectUrl() { $url = $this->redirector->getUrlGenerator(); if ($this->redirect) { return $url->to($this->redirect); } elseif ($this->redirectRoute) { return $url->route($this->redirectRoute); } elseif ($this->redirectAction) { return $url->action($this->redirectAction); } return $url->previous(); } /** * Set the Redirector instance. * * @param \Illuminate\Routing\Redirector $redirector * @return \Illuminate\Foundation\Http\FormRequest */ public function setRedirector(Redirector $redirector) { $this->redirector = $redirector; return $this; } /** * Set the container implementation. * * @param \Illuminate\Container\Container $container * @return $this */ public function setContainer(Container $container) { $this->container = $container; return $this; } /** * Set custom messages for validator errors. * * @return array */ public function messages() { return []; } /** * Set custom attributes for validator errors. * * @return array */ public function attributes() { return []; } }