Skip to content

Commit

Permalink
Initial "summarization" feature
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed Dec 4, 2020
1 parent aa1b323 commit 1e02bc2
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/Illuminate/Validation/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator as ValidatorFacade;
use Illuminate\Support\Str;

class ValidationException extends Exception
{
Expand Down Expand Up @@ -53,7 +54,7 @@ class ValidationException extends Exception
*/
public function __construct($validator, $response = null, $errorBag = 'default')
{
parent::__construct('The given data was invalid.');
parent::__construct(static::summarize($validator));

$this->response = $response;
$this->errorBag = $errorBag;
Expand All @@ -77,6 +78,32 @@ public static function withMessages(array $messages)
}));
}


/**
* Create a summary error message from the validation errors.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
*
* @return string
*/
protected static function summarize($validator)
{
$messages = $validator->errors()->all();

if (!count($messages)) {
return 'The given data was invalid.';
}

$message = array_shift($messages);

if ($additional = count($messages)) {
$pluralized = 1 === $additional ? 'error' : 'errors';
$message .= " (and $additional more $pluralized)";
}

return $message;
}

/**
* Get all of the validation error messages.
*
Expand Down
54 changes: 54 additions & 0 deletions tests/Validation/ValidationExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Illuminate\Tests\Validation;

use Illuminate\Translation\ArrayLoader;
use Illuminate\Translation\Translator;
use Illuminate\Validation\ValidationException;
use Illuminate\Validation\Validator;
use PHPUnit\Framework\TestCase;

class ValidationExceptionTest extends TestCase
{
public function testExceptionSummarizesZeroErrors()
{
$exception = $this->getException([], []);

$this->assertEquals('The given data was invalid.', $exception->getMessage());
}


public function testExceptionSummarizesOneError()
{
$exception = $this->getException([], ['foo' => 'required']);

$this->assertEquals('validation.required', $exception->getMessage());
}


public function testExceptionSummarizesTwoErrors()
{
$exception = $this->getException([], [ 'foo' => 'required', 'bar' => 'required' ]);

$this->assertEquals('validation.required (and 1 more error)', $exception->getMessage());
}

public function testExceptionSummarizesThreeOrMoreErrors()
{
$exception = $this->getException([], [
'foo' => 'required',
'bar' => 'required',
'baz' => 'required',
]);

$this->assertEquals('validation.required (and 2 more errors)', $exception->getMessage());
}

protected function getException($data = [], $rules = [])
{
$translator = new Translator(new ArrayLoader, 'en');
$validator = new Validator($translator, $data, $rules);

return new ValidationException($validator);
}
}

0 comments on commit 1e02bc2

Please sign in to comment.