ValidationException.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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|null
  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|null $response
  43. * @param string $errorBag
  44. */
  45. public function __construct($validator, $response = null, $errorBag = 'default')
  46. {
  47. parent::__construct(static::summarize($validator));
  48. $this->response = $response;
  49. $this->errorBag = $errorBag;
  50. $this->validator = $validator;
  51. }
  52. /**
  53. * Create a new validation exception from a plain array of messages.
  54. *
  55. * @param array $messages
  56. * @return static
  57. */
  58. public static function withMessages(array $messages)
  59. {
  60. return new static(tap(ValidatorFacade::make([], []), function ($validator) use ($messages) {
  61. foreach ($messages as $key => $value) {
  62. foreach (Arr::wrap($value) as $message) {
  63. $validator->errors()->add($key, $message);
  64. }
  65. }
  66. }));
  67. }
  68. /**
  69. * Create an error message summary from the validation errors.
  70. *
  71. * @param \Illuminate\Contracts\Validation\Validator $validator
  72. * @return string
  73. */
  74. protected static function summarize($validator)
  75. {
  76. $messages = $validator->errors()->all();
  77. if (! count($messages) || ! is_string($messages[0])) {
  78. return $validator->getTranslator()->get('The given data was invalid.');
  79. }
  80. $message = array_shift($messages);
  81. if ($count = count($messages)) {
  82. $pluralized = $count === 1 ? 'error' : 'errors';
  83. $message .= ' '.$validator->getTranslator()->choice("(and :count more $pluralized)", $count, compact('count'));
  84. }
  85. return $message;
  86. }
  87. /**
  88. * Get all of the validation error messages.
  89. *
  90. * @return array
  91. */
  92. public function errors()
  93. {
  94. return $this->validator->errors()->messages();
  95. }
  96. /**
  97. * Set the HTTP status code to be used for the response.
  98. *
  99. * @param int $status
  100. * @return $this
  101. */
  102. public function status($status)
  103. {
  104. $this->status = $status;
  105. return $this;
  106. }
  107. /**
  108. * Set the error bag on the exception.
  109. *
  110. * @param string $errorBag
  111. * @return $this
  112. */
  113. public function errorBag($errorBag)
  114. {
  115. $this->errorBag = $errorBag;
  116. return $this;
  117. }
  118. /**
  119. * Set the URL to redirect to on a validation error.
  120. *
  121. * @param string $url
  122. * @return $this
  123. */
  124. public function redirectTo($url)
  125. {
  126. $this->redirectTo = $url;
  127. return $this;
  128. }
  129. /**
  130. * Get the underlying response instance.
  131. *
  132. * @return \Symfony\Component\HttpFoundation\Response|null
  133. */
  134. public function getResponse()
  135. {
  136. return $this->response;
  137. }
  138. }