Skip to content

Commit

Permalink
Issue 4 - Show errors feedback on input groups
Browse files Browse the repository at this point in the history
  • Loading branch information
bgaze committed Dec 7, 2019
1 parent 83da540 commit a85bc4e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
13 changes: 8 additions & 5 deletions src/Inputs/CheckChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
class CheckChoice extends Input
{

/**
* The format to use to display error messages.
*/
const ERRORS_FORMAT = '<div class="invalid-feedback d-block">:message</div>';

/**
* Get the input default options.
*
Expand Down Expand Up @@ -82,6 +77,14 @@ protected function configureLabel($label, array $options)
}
}

/**
* Get the template to build up error messages.
*/
protected function errorTemplate()
{
return '<div class="invalid-feedback d-block">:message</div>';
}

/**
* Compile input to a HTML string.
*
Expand Down
18 changes: 10 additions & 8 deletions src/Support/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ abstract class Input

use HasSettings;

/**
* The format to use to display error messages.
*/
const ERRORS_FORMAT = '<div class="invalid-feedback">:message</div>';

/**
* Illuminate HtmlBuilder instance.
*
Expand Down Expand Up @@ -190,10 +185,17 @@ protected function configureGroup(array $options)
}
}

/**
* Get the template to build up error messages.
*/
protected function errorTemplate()
{
return '<div class="invalid-feedback">:message</div>';
}

/**
* Compile inputs errors to and HTML string.
* Add error class to input and group if there is any errors.
*
*/
protected function getErrors()
{
Expand All @@ -215,9 +217,9 @@ protected function getErrors()
}

if ($this->show_all_errors) {
$this->errors = implode('', $errorBag->get($field, static::ERRORS_FORMAT));
$this->errors = implode('', $errorBag->get($field, $this->errorTemplate()));
} else {
$this->errors = $errorBag->first($field, static::ERRORS_FORMAT);
$this->errors = $errorBag->first($field, $this->errorTemplate());
}

$this->input_attributes->addClass('is-invalid');
Expand Down
12 changes: 12 additions & 0 deletions src/Support/Traits/HasAddons.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
trait HasAddons
{

/**
* Get the template to build up error messages.
*/
protected function errorTemplate()
{
if ($this->append || $this->prepend) {
return '<div class="invalid-feedback d-block">:message</div>';
}

return '<div class="invalid-feedback">:message</div>';
}

/**
* Build a Bootstrap input group if append or prepend options are provided.
*
Expand Down

0 comments on commit a85bc4e

Please sign in to comment.