Skip to content

Commit

Permalink
Merge pull request #5 from mindplay-dk/error-summary
Browse files Browse the repository at this point in the history
add basic error-summary renderer
  • Loading branch information
mindplay-dk authored Mar 7, 2018
2 parents a45428d + 990a48a commit 13e731e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ This library has other expected and useful features, including:

* Default error messages can be localized/customized.

* A basic error-summary can be generated with `InputRenderer::errorSummary()`.

It deliberately does not implement any of the following:

* Trivial elements: things like `<form>`, `<fieldset>` and `<legend>` - you don't
Expand Down
12 changes: 3 additions & 9 deletions examples/demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public function __construct(TokenServiceInterface $token_service)

$form = new InputRenderer($model, 'form');

$form->error_summary_class = "alert alert-danger"; // this example uses a basic bootstrap alert for the error-summary

?>
<!DOCTYPE html>
<html lang="en">
Expand Down Expand Up @@ -131,15 +133,7 @@ public function __construct(TokenServiceInterface $token_service)
</form>
<?php endif ?>

<?php if ($model->hasErrors()): ?>
<div class="alert alert-danger">
<ul>
<?php foreach ($model->getErrors() as $error): ?>
<li><?= $error ?></li>
<?php endforeach ?>
</ul>
</div>
<?php endif ?>
<?= $form->errorSummary() ?>

</div>

Expand Down
43 changes: 43 additions & 0 deletions src/InputRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ class InputRenderer
*/
public $group_attrs = ['class' => 'form-group'];

/**
* @var string error summary CSS class-name
*
* @see errorSummary()
*/
public $error_summary_class = "error-summary";

/**
* @var string[] map where Field name => label override
*/
Expand Down Expand Up @@ -708,4 +715,40 @@ public function labelFor(FieldInterface $field, $label = null, array $attr = [])

return $this->label($id, $label);
}

/**
* Build an HTML error summary, if there are any errors. (otherwise, returns an empty string.)
*
* Typically this is used at the end of a form (above the submit button) to call
* attention to the fact that some inputs failed validation - this is meaningful
* on long forms with many inputs, where the user might not immediately notice
* a single input that has been highlighted with an error state.
*
* @return string error summary (or an empty string, if the current model contains no errors)
*/
public function errorSummary()
{
if (! $this->model->hasErrors()) {
return "";
}

$errors = implode(
"",
array_map(
function ($str) {
return "<li>" . $this->escape($str) . "</li>";
},
$this->model->getErrors()
)
);

return $this->tag(
"div",
[
"role" => "alert",
"class" => $this->error_summary_class
],
"<ul>{$errors}</ul>"
);
}
}
18 changes: 18 additions & 0 deletions tests/unit/InputRendererCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,22 @@ public function buildInput(UnitTester $I)

$I->assertSame('<input data-bat="baz" name="foo" type="hidden" value="bar"/>', $form->input("hidden", "foo", "bar", ["data-bat" => "baz"]));
}

public function buildErrorSummary(UnitTester $I)
{
$form = new InputRenderer();

$field = new TextField("test");

$field->setLabel("Something");

$expected_error_message = "Unacceptable, Sir!";

$form->model->setError($field, $expected_error_message);

$I->assertSame(
"<div class=\"error-summary\" role=\"alert\"><ul><li>{$expected_error_message}</li></ul></div>",
$form->errorSummary()
);
}
}

0 comments on commit 13e731e

Please sign in to comment.