Skip to content

Commit

Permalink
Add ErrorSummary::onlyCommonErrors() (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Nov 15, 2023
1 parent 52f2024 commit f9c79fa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Field/ErrorSummary.php
Expand Up @@ -65,6 +65,16 @@ public function onlyAttributes(string ...$names): self
return $new;
}

/**
* Use only common errors when rendering the error summary.
*/
public function onlyCommonErrors(): self
{
$new = clone $this;
$new->onlyAttributes = [''];
return $new;
}

/**
* Set the footer text for the error summary
*/
Expand Down
27 changes: 27 additions & 0 deletions tests/Field/ErrorSummaryTest.php
Expand Up @@ -89,6 +89,32 @@ public function testShowAllErrors(): void
$this->assertSame($expected, $result);
}

public function testOnlyCommonErrors(): void
{
$form = ErrorSummaryForm::validated();
$form->getValidationResult()
->addError('Common error 1')
->addError('Common error 2');

$result = ErrorSummary::widget()
->formModel($form)
->onlyCommonErrors()
->showAllErrors()
->render();

$expected = <<<HTML
<div>
<p>Please fix the following errors:</p>
<ul>
<li>Common error 1</li>
<li>Common error 2</li>
</ul>
</div>
HTML;

$this->assertSame($expected, $result);
}

public function testFooter(): void
{
$result = ErrorSummary::widget()
Expand Down Expand Up @@ -149,6 +175,7 @@ public function testImmutability(): void
$this->assertNotSame($field, $field->encode(false));
$this->assertNotSame($field, $field->showAllErrors());
$this->assertNotSame($field, $field->onlyAttributes());
$this->assertNotSame($field, $field->onlyCommonErrors());
$this->assertNotSame($field, $field->header(''));
$this->assertNotSame($field, $field->headerAttributes([]));
$this->assertNotSame($field, $field->footer(''));
Expand Down

0 comments on commit f9c79fa

Please sign in to comment.