Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ErrorSummary::listClass() and ErrorSummary::addListClass() #274

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"php": "^8.0",
"ext-mbstring": "*",
"yiisoft/friendly-exception": "^1.0",
"yiisoft/html": "^3.0",
"yiisoft/html": "^3.2",
"yiisoft/http": "^1.2",
"yiisoft/hydrator": "dev-master",
"yiisoft/hydrator-validator": "dev-master",
Expand Down
24 changes: 24 additions & 0 deletions src/Field/ErrorSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,30 @@ public function listAttributes(array $attributes): self
return $new;
}

/**
* Add one or more CSS classes to the list container tag.
*
* @param string|null ...$class One or many CSS classes.
*/
public function addListClass(?string ...$class): self
{
$new = clone $this;
Html::addCssClass($new->listAttributes, $class);
return $new;
}

/**
* Replace current list container tag CSS classes with a new set of classes.
*
* @param string|null ...$class One or many CSS classes.
*/
public function listClass(?string ...$class): static
{
$new = clone $this;
$new->listAttributes['class'] = array_filter($class, static fn ($c) => $c !== null);
return $new;
}

protected function generateContent(): ?string
{
$messages = $this->collectErrors();
Expand Down
44 changes: 44 additions & 0 deletions tests/Field/ErrorSummaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,48 @@ public function testListAttributes(): void
$this->assertSame($expected, $result);
}

public function testListClass(): void
{
$result = ErrorSummary::widget()
->formModel(ErrorSummaryForm::validated())
->onlyAttributes('year')
->listAttributes(['class' => 'list'])
->listClass('errorsList')
->render();

$expected = <<<HTML
<div>
<p>Please fix the following errors:</p>
<ul class="errorsList">
<li>Bad year.</li>
</ul>
</div>
HTML;

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

public function testAddListClass(): void
{
$result = ErrorSummary::widget()
->formModel(ErrorSummaryForm::validated())
->onlyAttributes('year')
->listClass('errorsList')
->addListClass('errorsList-tiny')
->render();

$expected = <<<HTML
<div>
<p>Please fix the following errors:</p>
<ul class="errorsList errorsList-tiny">
<li>Bad year.</li>
</ul>
</div>
HTML;

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

public function testWithoutForm(): void
{
$field = ErrorSummary::widget();
Expand All @@ -201,6 +243,8 @@ public function testImmutability(): void
$this->assertNotSame($field, $field->header(''));
$this->assertNotSame($field, $field->headerAttributes([]));
$this->assertNotSame($field, $field->listAttributes([]));
$this->assertNotSame($field, $field->listClass());
$this->assertNotSame($field, $field->addListClass());
$this->assertNotSame($field, $field->footer(''));
$this->assertNotSame($field, $field->footerAttributes([]));
}
Expand Down