ValidationException.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace Illuminate\Validation;
  3. use Exception;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Support\Facades\Validator as ValidatorFacade;
  6. class ValidationException extends Exception
  7. {
  8. /**
  9. * The validator instance.
  10. *
  11. * @var \Illuminate\Contracts\Validation\Validator
  12. */
  13. public $validator;
  14. /**
  15. * The recommended response to send to the client.
  16. *
  17. * @var \Symfony\Component\HttpFoundation\Response|null
  18. */
  19. public $response;
  20. /**
  21. * The status code to use for the response.
  22. *
  23. * @var int
  24. */
  25. public $status = 422;
  26. /**
  27. * The name of the error bag.
  28. *
  29. * @var string
  30. */
  31. public $errorBag;
  32. /**
  33. * The path the client should be redirected to.
  34. *
  35. * @var string
  36. */
  37. public $redirectTo;
  38. /**
  39. * Create a new exception instance.
  40. *
  41. * @param \Illuminate\Contracts\Validation\Validator $validator
  42. * @param \Symfony\Component\HttpFoundation\Response $response
  43. * @param string $errorBag
  44. * @return void
  45. */
  46. public function __construct($validator, $response = null, $errorBag = 'default')
  47. {
  48. parent::__construct('The given data was invalid.');
  49. $this->response = $response;
  50. $this->errorBag = $errorBag;
  51. $this->validator = $validator;
  52. }
  53. /**
  54. * Create a new validation exception from a plain array of messages.
  55. *
  56. * @param array $messages
  57. * @return static
  58. */
  59. public static function withMessages(array $messages)
  60. {
  61. return new static(tap(ValidatorFacade::make([], []), function ($validator) use ($messages) {
  62. foreach ($messages as $key => $value) {
  63. foreach (Arr::wrap($value) as $message) {
  64. $validator->errors()->add($key, $message);
  65. }
  66. }
  67. }));
  68. }
  69. /**
  70. * Get all of the validation error messages.
  71. *
  72. * @return array
  73. */
  74. public function errors()
  75. {
  76. return $this->validator->errors()->messages();
  77. }
  78. /**
  79. * Set the HTTP status code to be used for the response.
  80. *
  81. * @param int $status
  82. * @return $this
  83. */
  84. public function status($status)
  85. {
  86. $this->status = $status;
  87. return $this;
  88. }
  89. /**
  90. * Set the error bag on the exception.
  91. *
  92. * @param string $errorBag
  93. * @return $this
  94. */
  95. public function errorBag($errorBag)
  96. {
  97. $this->errorBag = $errorBag;
  98. return $this;
  99. }
  100. /**
  101. * Set the URL to redirect to on a validation error.
  102. *
  103. * @param string $url
  104. * @return $this
  105. */
  106. public function redirectTo($url)
  107. {
  108. $this->redirectTo = $url;
  109. return $this;
  110. }
  111. /**
  112. * Get the underlying response instance.
  113. *
  114. * @return \Symfony\Component\HttpFoundation\Response|null
  115. */
  116. public function getResponse()
  117. {
  118. return $this->response;
  119. }
  120. }