Skip to content

Commit

Permalink
Group errors into a single div, separated by spans
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Feb 22, 2024
1 parent 32a8759 commit 51fed53
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions src/Fieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,10 +496,12 @@ protected function prepareTable(): void
$table = new Child('table');

foreach ($fields as $field) {
$errors = [];
$errors = null;
if ($field->hasErrors()) {
$errors = new Child('div');
$errors->setAttribute('class', 'error');
foreach ($field->getErrors() as $error) {
$errors[] = (new Child('div', $error))->setAttribute('class', 'error');
$errors->addChild(new Child('span', $error));
}
}

Expand Down Expand Up @@ -536,8 +538,8 @@ protected function prepareTable(): void

$td = new Child('td', $nodeValue, $options);

if ($field->isErrorPre()) {
$td->addChildren($errors);
if (!empty($errors) && ($field->isErrorPre())) {
$td->addChild($errors);
}
$td->addChild($field);

Expand All @@ -552,8 +554,8 @@ protected function prepareTable(): void
if ($field->hasLabel()) {
$td->setAttribute('colspan', 2);
}
if (!$field->isErrorPre()) {
$td->addChildren($errors);
if (!empty($errors) && (!$field->isErrorPre())) {
$td->addChild($errors);
}
$tr->addChild($td);
$table->addChild($tr);
Expand All @@ -573,10 +575,12 @@ protected function prepareElement(string $element): void
{
foreach ($this->fields as $fields) {
foreach ($fields as $field) {
$errors = [];
$errors = null;
if ($field->hasErrors()) {
$errors = new Child('div');
$errors->setAttribute('class', 'error');
foreach ($field->getErrors() as $error) {
$errors[] = (new Child('div', $error))->setAttribute('class', 'error');
$errors->addChild(new Child('span', $error));
}
}

Expand Down Expand Up @@ -609,8 +613,8 @@ protected function prepareElement(string $element): void
$container->addChild($label);
}

if ($field->isErrorPre()) {
$container->addChildren($errors);
if (!empty($errors) && ($field->isErrorPre())) {
$container->addChild($errors);
}
$container->addChild($field);

Expand All @@ -621,8 +625,8 @@ protected function prepareElement(string $element): void
}
$container->addChild($hint);
}
if (!$field->isErrorPre()) {
$container->addChildren($errors);
if (!empty($errors) && (!$field->isErrorPre())) {
$container->addChild($errors);
}
$this->addChild($container);
}
Expand All @@ -640,10 +644,12 @@ protected function prepareDl(): void
$dl = new Child('dl');

foreach ($fields as $field) {
$errors = [];
$errors = null;
if ($field->hasErrors()) {
$errors = new Child('div');
$errors->setAttribute('class', 'error');
foreach ($field->getErrors() as $error) {
$errors[] = (new Child('div', $error))->setAttribute('class', 'error');
$errors->addChild(new Child('span', $error));
}
}

Expand Down Expand Up @@ -679,8 +685,8 @@ protected function prepareDl(): void

$dd = new Child('dd', $nodeValue, $options);

if ($field->isErrorPre()) {
$dd->addChildren($errors);
if (!empty($errors) && ($field->isErrorPre())) {
$dd->addChild($errors);
}
$dd->addChild($field);

Expand All @@ -691,8 +697,8 @@ protected function prepareDl(): void
}
$dd->addChild($hint);
}
if (!$field->isErrorPre()) {
$dd->addChildren($errors);
if (!empty($errors) && (!$field->isErrorPre())) {
$dd->addChild($errors);
}
$dl->addChild($dd);
}
Expand Down

0 comments on commit 51fed53

Please sign in to comment.