@endif
@include('bootstrap-components::bootstrap-components.partials.prepend')
-
+
@include('bootstrap-components::bootstrap-components.partials.append')
@include('bootstrap-components::bootstrap-components.partials.validation-feedback')
@if(! empty($prepend) || ! empty($append))
diff --git a/resources/views/bootstrap-components/partials/validation-feedback.blade.php b/resources/views/bootstrap-components/partials/validation-feedback.blade.php
index 5c005353..75873b32 100644
--- a/resources/views/bootstrap-components/partials/validation-feedback.blade.php
+++ b/resources/views/bootstrap-components/partials/validation-feedback.blade.php
@@ -1,11 +1,9 @@
-@isset($errors)
- @if($errorMessage && $displayFailure)
-
- {!! $errorMessage !!}
-
- @elseif($displaySuccess)
-
- @lang('Field correctly filled.')
-
- @endif
-@endisset
+@if($errorMessage = $errorMessage($errors ?? null, $locale ?? null))
+
+ {!! $errorMessage !!}
+
+@elseif($successMessage = $successMessage())
+
+ {!! $successMessage !!}
+
+@endif
diff --git a/src/Components/Buttons/Abstracts/ButtonAbstract.php b/src/Components/Buttons/Abstracts/ButtonAbstract.php
index ef476d0c..5170342f 100644
--- a/src/Components/Buttons/Abstracts/ButtonAbstract.php
+++ b/src/Components/Buttons/Abstracts/ButtonAbstract.php
@@ -26,11 +26,9 @@ public function route(string $route, array $params = []): self
return $this;
}
- protected function getParameters(): array
+ protected function getViewParams(): array
{
- $url = $this->getUrl();
-
- return array_merge(parent::getParameters(), compact('url'));
+ return array_merge(parent::getViewParams(), ['url' => $this->getUrl()]);
}
protected function getUrl(): ?string
diff --git a/src/Components/Buttons/Abstracts/SubmitAbstract.php b/src/Components/Buttons/Abstracts/SubmitAbstract.php
index 32b49540..883d99c8 100644
--- a/src/Components/Buttons/Abstracts/SubmitAbstract.php
+++ b/src/Components/Buttons/Abstracts/SubmitAbstract.php
@@ -41,20 +41,15 @@ public function label(?string $label): self
return $this;
}
- protected function getValues(): array
+ protected function getViewParams(): array
{
- return array_merge(parent::getValues(), $this->getParameters());
- }
-
- protected function getParameters(): array
- {
- $type = $this->getType();
- $url = null;
- $prepend = $this->getPrepend();
- $append = $this->getAppend();
- $label = $this->getLabel();
-
- return compact('type', 'url', 'prepend', 'append', 'label');
+ return array_merge(parent::getViewParams(), [
+ 'type' => $this->getType(),
+ 'url' => $this->getType(),
+ 'prepend' => $this->getPrepend(),
+ 'append' => $this->getAppend(),
+ 'label' => $this->getLabel(),
+ ]);
}
protected function getPrepend(): ?string
diff --git a/src/Components/ComponentAbstract.php b/src/Components/ComponentAbstract.php
index 84a6e10e..aef4cdc0 100644
--- a/src/Components/ComponentAbstract.php
+++ b/src/Components/ComponentAbstract.php
@@ -32,56 +32,6 @@ public function __construct()
$this->containerHtmlAttributes = $this->setContainerHtmlAttributes();
}
- public function containerId(string $containerId): self
- {
- $this->containerId = $containerId;
-
- return $this;
- }
-
- public function componentId(string $componentId): self
- {
- $this->componentId = $componentId;
-
- return $this;
- }
-
- public function componentClasses(array $componentClasses, bool $mergeMode = false): self
- {
- $this->componentClasses = $mergeMode
- ? array_merge($this->componentClasses, $componentClasses)
- : $componentClasses;
-
- return $this;
- }
-
- public function containerClasses(array $containerClasses, bool $mergeMode = false): self
- {
- $this->containerClasses = $mergeMode
- ? array_merge($this->containerClasses, $containerClasses)
- : $containerClasses;
-
- return $this;
- }
-
- public function componentHtmlAttributes(array $componentHtmlAttributes, bool $mergeMode = false): self
- {
- $this->componentHtmlAttributes = $mergeMode
- ? array_merge($this->componentHtmlAttributes, $componentHtmlAttributes)
- : $componentHtmlAttributes;
-
- return $this;
- }
-
- public function containerHtmlAttributes(array $containerHtmlAttributes, bool $mergeMode = false): self
- {
- $this->containerHtmlAttributes = $mergeMode
- ? array_merge($this->containerHtmlAttributes, $containerHtmlAttributes)
- : $containerHtmlAttributes;
-
- return $this;
- }
-
/**
* @return string
* @throws \Throwable
@@ -102,7 +52,7 @@ public function render(array $extraData = []): string
$this->checkValuesValidity();
$view = $this->getView();
$html = $view
- ? (string) view('bootstrap-components::' . $view, array_merge($this->getValues(), $extraData))->render()
+ ? (string) view('bootstrap-components::' . $view, array_merge($this->getViewParams(), $extraData))->render()
: '';
return trim($html);
@@ -117,23 +67,21 @@ protected function getView(): string
abstract protected function setView(): string;
- protected function getValues(): array
+ protected function getViewParams(): array
{
- $componentId = $this->getComponentId();
- $containerId = $this->getContainerId();
- $componentClasses = $this->getComponentClasses();
- $containerClasses = $this->getContainerClasses();
- $componentHtmlAttributes = $this->getComponentHtmlAttributes();
- $containerHtmlAttributes = $this->getContainerHtmlAttributes();
+ return [
+ 'containerId' => $this->getContainerId(),
+ 'containerClasses' => $this->getContainerClasses(),
+ 'containerHtmlAttributes' => $this->getContainerHtmlAttributes(),
+ 'componentId' => $this->getComponentId(),
+ 'componentClasses' => $this->getComponentClasses(),
+ 'componentHtmlAttributes' => $this->getComponentHtmlAttributes(),
+ ];
+ }
- return compact(
- 'componentId',
- 'containerId',
- 'componentClasses',
- 'containerClasses',
- 'componentHtmlAttributes',
- 'containerHtmlAttributes'
- );
+ protected function getContainerId(): ?string
+ {
+ return $this->containerId;
}
protected function getComponentId(): ?string
@@ -141,11 +89,13 @@ protected function getComponentId(): ?string
return $this->componentId;
}
- protected function getContainerId(): ?string
+ protected function getContainerClasses(): array
{
- return $this->containerId;
+ return $this->containerClasses;
}
+ abstract protected function setContainerClasses(): array;
+
protected function getComponentClasses(): array
{
return $this->componentClasses;
@@ -153,12 +103,12 @@ protected function getComponentClasses(): array
abstract protected function setComponentClasses(): array;
- protected function getContainerClasses(): array
+ protected function getContainerHtmlAttributes(): array
{
- return $this->containerClasses;
+ return $this->containerHtmlAttributes;
}
- abstract protected function setContainerClasses(): array;
+ abstract protected function setContainerHtmlAttributes(): array;
protected function getComponentHtmlAttributes(): array
{
@@ -167,12 +117,55 @@ protected function getComponentHtmlAttributes(): array
abstract protected function setComponentHtmlAttributes(): array;
- protected function getContainerHtmlAttributes(): array
+ public function containerId(string $containerId): self
{
- return $this->containerHtmlAttributes;
+ $this->containerId = $containerId;
+
+ return $this;
}
- abstract protected function setContainerHtmlAttributes(): array;
+ public function componentId(string $componentId): self
+ {
+ $this->componentId = $componentId;
+
+ return $this;
+ }
+
+ public function componentClasses(array $componentClasses, bool $mergeMode = false): self
+ {
+ $this->componentClasses = $mergeMode
+ ? array_merge($this->componentClasses, $componentClasses)
+ : $componentClasses;
+
+ return $this;
+ }
+
+ public function containerClasses(array $containerClasses, bool $mergeMode = false): self
+ {
+ $this->containerClasses = $mergeMode
+ ? array_merge($this->containerClasses, $containerClasses)
+ : $containerClasses;
+
+ return $this;
+ }
+
+ public function componentHtmlAttributes(array $componentHtmlAttributes, bool $mergeMode = false): self
+ {
+ $this->componentHtmlAttributes = $mergeMode
+ ? array_merge($this->componentHtmlAttributes, $componentHtmlAttributes)
+ : $componentHtmlAttributes;
+
+ return $this;
+ }
+
+ public function containerHtmlAttributes(array $containerHtmlAttributes, bool $mergeMode = false): self
+ {
+ $this->containerHtmlAttributes = $mergeMode
+ ? array_merge($this->containerHtmlAttributes, $containerHtmlAttributes)
+ : $containerHtmlAttributes;
+
+ return $this;
+ }
protected function getType(): string
{
diff --git a/src/Components/Form/Abstracts/CheckableAbstract.php b/src/Components/Form/Abstracts/CheckableAbstract.php
index b921d57a..30d7b782 100644
--- a/src/Components/Form/Abstracts/CheckableAbstract.php
+++ b/src/Components/Form/Abstracts/CheckableAbstract.php
@@ -15,18 +15,28 @@ public function checked(bool $checked = true): self
protected function setLabelPositionedAbove(): bool
{
- return true; // Unused for checkable components.
+ // Setting `true` but this property will not be used for checkable components.
+ return true;
}
protected function getComponentHtmlAttributes(): array
{
- return array_merge(parent::getComponentHtmlAttributes(), $this->getChecked() ? ['checked' => 'checked'] : []);
+ return array_merge($this->componentHtmlAttributes, $this->getChecked() ? ['checked' => 'checked'] : []);
}
protected function getChecked(): bool
{
- $old = old($this->convertArrayNameInNotation());
-
- return $old ?? $this->checked ?? (bool) (optional($this->model)->{$this->getName()} ?: $this->value);
+ $oldChecked = old($this->convertArrayNameInNotation());
+ if (isset($oldChecked)) {
+ return $oldChecked;
+ }
+ if (isset($this->checked)) {
+ return $this->checked;
+ }
+ if (isset($this->value)) {
+ return (bool) $this->value;
+ }
+
+ return (bool) optional($this->model)->{$this->getName()};
}
}
diff --git a/src/Components/Form/Abstracts/FormAbstract.php b/src/Components/Form/Abstracts/FormAbstract.php
index 7afcd78a..86965d2e 100644
--- a/src/Components/Form/Abstracts/FormAbstract.php
+++ b/src/Components/Form/Abstracts/FormAbstract.php
@@ -5,6 +5,7 @@
use Closure;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
+use Illuminate\Support\ViewErrorBag;
use Okipa\LaravelBootstrapComponents\Components\ComponentAbstract;
use Okipa\LaravelBootstrapComponents\Components\Form\Traits\FormValidityChecks;
@@ -12,6 +13,9 @@ abstract class FormAbstract extends ComponentAbstract
{
use FormValidityChecks;
+ /** @property mixed $value */
+ protected $value;
+
protected ?Model $model = null;
protected string $name;
@@ -30,10 +34,7 @@ abstract class FormAbstract extends ComponentAbstract
protected bool $labelPositionedAbove;
- /** @property mixed $value */
- protected $value;
-
- protected ?string $placeholder;
+ protected ?string $placeholder = null;
protected bool $displaySuccess;
@@ -46,6 +47,7 @@ public function __construct()
$this->append = $this->setAppend();
$this->labelPositionedAbove = $this->setLabelPositionedAbove();
$this->caption = $this->setCaption();
+ $this->errors = new ViewErrorBag();
$this->displaySuccess = $this->setDisplaySuccess();
$this->displayFailure = $this->setDisplayFailure();
}
@@ -102,6 +104,11 @@ public function placeholder(?string $placeholder): self
return $this;
}
+ /**
+ * @param mixed $value
+ *
+ * @return $this
+ */
public function value($value): self
{
$this->value = $value;
@@ -138,49 +145,25 @@ public function displayFailure(bool $displayFailure = true): self
return $this;
}
- protected function getValues(): array
+ protected function getValidationClass(?ViewErrorBag $errors): ?string
{
- return array_merge(parent::getValues(), $this->getParameters());
- }
+ if (! $errors) {
+ return null;
+ }
+ if ($errors->isEmpty()) {
+ return null;
+ }
+ if ($errors->has($this->convertArrayNameInNotation())) {
+ return $this->getDisplayFailure() ? 'is-invalid' : null;
+ }
- protected function getParameters(): array
- {
- $model = $this->getModel();
- $type = $this->getType();
- $name = $this->getName();
- $prepend = $this->getPrepend();
- $append = $this->getAppend();
- $caption = $this->getCaption();
- $label = $this->getLabel();
- $labelPositionedAbove = $this->getLabelPositionedAbove();
- $value = $this->getValue();
- $placeholder = $this->getPlaceholder();
- $displaySuccess = $this->getDisplaySuccess();
- $displayFailure = $this->getDisplayFailure();
- $validationClass = $this->getValidationClass();
- $errorMessage = $this->getErrorMessage();
-
- return compact(
- 'model',
- 'type',
- 'name',
- 'prepend',
- 'append',
- 'caption',
- 'label',
- 'labelPositionedAbove',
- 'value',
- 'placeholder',
- 'displaySuccess',
- 'displayFailure',
- 'validationClass',
- 'errorMessage'
- );
+ // Only highlight valid fields if there are invalid fields.
+ return $this->getDisplaySuccess() ? 'is-valid' : null;
}
- protected function getModel(): ?Model
+ protected function convertArrayNameInNotation(string $notation = '.'): string
{
- return $this->model;
+ return str_replace(['[', ']'], [$notation, ''], $this->getName());
}
protected function getName(): string
@@ -188,43 +171,66 @@ protected function getName(): string
return $this->name ?? '';
}
- protected function getPrepend(): ?string
+ protected function getDisplayFailure(): bool
{
- $prepend = $this->prepend;
-
- // Fallback for usage of closure with multilingual disabled.
- return $prepend instanceof Closure ? $prepend(app()->getLocale()) : $prepend;
+ return $this->displayFailure;
}
- abstract protected function setPrepend(): ?string;
+ abstract protected function setDisplayFailure(): bool;
- protected function getAppend(): ?string
+ protected function getDisplaySuccess(): bool
{
- $append = $this->append;
-
- // Fallback for usage of closure with multilingual disabled.
- return $append instanceof Closure ? $append(app()->getLocale()) : $append;
+ return $this->displaySuccess;
}
- abstract protected function setAppend(): ?string;
+ abstract protected function setDisplaySuccess(): bool;
- protected function getCaption(): ?string
+ protected function getErrorMessage(?ViewErrorBag $errors): ?string
{
- return $this->caption;
- }
+ if (! $errors) {
+ return null;
+ }
+ if (! $this->getDisplayFailure()) {
+ return null;
+ }
- abstract protected function setCaption(): ?string;
+ return $errors->first($this->convertArrayNameInNotation());
+ }
- protected function getLabel(): ?string
+ protected function getSuccessMessage(): ?string
{
- $label = $this->label ?: (string) __('validation.attributes.' . $this->removeArrayCharactersFromName());
+ if ($this->getDisplaySuccess()) {
+ return (string) __('Field correctly filled.');
+ }
- return $this->hideLabel ? null : $label;
+ return null;
}
- protected function removeArrayCharactersFromName(): string
+ protected function getViewParams(): array
{
- return strstr($this->getName(), '[', true) ?: $this->getName();
+ return array_merge(parent::getViewParams(), [
+ 'validationClass' => fn(?ViewErrorBag $errors) => $this->getValidationClass($errors),
+ 'errorMessage' => fn(?ViewErrorBag $errors) => $this->getErrorMessage($errors),
+ 'successMessage' => fn() => $this->getSuccessMessage(),
+ 'labelPositionedAbove' => $this->getLabelPositionedAbove(),
+ 'label' => $this->getLabel(),
+ 'type' => $this->getType(),
+ 'name' => $this->getName(),
+ 'value' => $this->getValue(),
+ 'placeholder' => $this->getPlaceholder(),
+ 'prepend' => $this->getPrepend(),
+ 'append' => $this->getAppend(),
+ 'caption' => $this->getCaption(),
+ ]);
+ }
+
+ protected function getComponentId(): string
+ {
+ if ($this->componentId) {
+ return $this->componentId;
+ }
+
+ return $this->getType() . '-' . Str::slug(Str::snake($this->convertArrayNameInNotation('-'), '-'));
}
protected function getLabelPositionedAbove(): bool
@@ -234,59 +240,87 @@ protected function getLabelPositionedAbove(): bool
abstract protected function setLabelPositionedAbove(): bool;
- protected function getValue()
+ protected function getLabel(): ?string
{
- $value = old($this->convertArrayNameInNotation()) ?: $this->value;
- // Fallback for usage of closure with multilingual disabled.
- $value = $value instanceof Closure ? $value(app()->getLocale()) : $value;
+ if ($this->hideLabel) {
+ return null;
+ }
+ if ($this->label) {
+ return $this->label;
+ }
- return $value ?? optional($this->getModel())->{$this->convertArrayNameInNotation()};
+ return (string) __('validation.attributes.' . $this->removeArrayCharactersFromName());
}
- protected function convertArrayNameInNotation(string $notation = '.'): string
+ protected function removeArrayCharactersFromName(): string
{
- return str_replace(['[', ']'], [$notation, ''], $this->getName());
+ return strstr($this->getName(), '[', true) ?: $this->getName();
}
- protected function getPlaceholder(): ?string
+ /** @return mixed */
+ protected function getValue()
{
- return $this->placeholder
- ?? ($this->getLabel() ?: (string) __('validation.attributes.' . $this->removeArrayCharactersFromName()));
+ $oldValue = old($this->convertArrayNameInNotation());
+ if ($oldValue) {
+ return $oldValue;
+ }
+ // Fallback for usage of closure with multilingual disabled.
+ if ($this->value instanceof Closure) {
+ return ($this->value)(app()->getLocale());
+ }
+ if (isset($this->value)) {
+ return $this->value;
+ }
+
+ return optional($this->model)->{$this->convertArrayNameInNotation()};
}
- protected function getDisplaySuccess(): bool
+ protected function getPlaceholder(): ?string
{
- return $this->displaySuccess;
- }
+ if (isset($this->placeholder)) {
+ return $this->placeholder;
+ }
+ $label = $this->getLabel();
+ if ($label) {
+ return $label;
+ }
- abstract protected function setDisplaySuccess(): bool;
+ return (string) __('validation.attributes.' . $this->removeArrayCharactersFromName());
+ }
- protected function getDisplayFailure(): bool
+ protected function getPrepend(): ?string
{
- return $this->displayFailure;
+ // Fallback for usage of closure with multilingual disabled.
+ if ($this->prepend instanceof Closure) {
+ return ($this->prepend)(app()->getLocale());
+ }
+
+ return $this->prepend;
}
- abstract protected function setDisplayFailure(): bool;
+ abstract protected function setPrepend(): ?string;
- protected function getValidationClass(): ?string
+ protected function getAppend(): ?string
{
- if (session()->has('errors')) {
- return session()->get('errors')->has($this->convertArrayNameInNotation())
- ? ($this->getDisplayFailure() ? 'is-invalid' : null)
- : ($this->getDisplaySuccess() ? 'is-valid' : null);
+ // Fallback for usage of closure with multilingual disabled.
+ if ($this->append instanceof Closure) {
+ return ($this->append)(app()->getLocale());
}
- return null;
+ return $this->append;
}
- protected function getErrorMessage(): ?string
+ abstract protected function setAppend(): ?string;
+
+ protected function getCaption(): ?string
{
- return optional(session()->get('errors'))->first($this->convertArrayNameInNotation());
+ return $this->caption;
}
- protected function getComponentId(): string
+ abstract protected function setCaption(): ?string;
+
+ protected function getModel(): ?Model
{
- return parent::getComponentId()
- ?? $this->getType() . '-' . Str::slug(Str::snake($this->convertArrayNameInNotation('-'), '-'));
+ return $this->model;
}
}
diff --git a/src/Components/Form/Abstracts/MultilingualAbstract.php b/src/Components/Form/Abstracts/MultilingualAbstract.php
index 3b02cea6..6cbd9e05 100644
--- a/src/Components/Form/Abstracts/MultilingualAbstract.php
+++ b/src/Components/Form/Abstracts/MultilingualAbstract.php
@@ -3,13 +3,11 @@
namespace Okipa\LaravelBootstrapComponents\Components\Form\Abstracts;
use Closure;
+use Illuminate\Support\ViewErrorBag;
use Okipa\LaravelBootstrapComponents\Components\Form\Multilingual\Resolver;
-use Okipa\LaravelBootstrapComponents\Components\Form\Traits\MultilingualValidityChecks;
abstract class MultilingualAbstract extends FormAbstract
{
- use MultilingualValidityChecks;
-
protected Resolver $multilingualResolver;
protected array $locales;
@@ -59,7 +57,7 @@ protected function multilingualRender(array $extraData = []): string
$componentHtml = $view
? (string) view(
'bootstrap-components::' . $view,
- array_merge($this->getLocalizedValues($locale), $extraData)
+ array_merge($this->getLocalizedViewParams($locale), $extraData)
)->render()
: '';
$html .= trim($componentHtml);
@@ -68,39 +66,41 @@ protected function multilingualRender(array $extraData = []): string
return $html;
}
- protected function getLocalizedValues(string $locale): array
- {
- return array_merge($this->getValues(), $this->getLocalizedParameters($locale));
+ protected function getLocalizedViewParams(string $locale): array
+ {
+ return [
+ 'component' => $this,
+ 'locale' => $locale,
+ 'containerId' => $this->getLocalizedContainerId($locale),
+ 'containerClasses' => $this->getContainerClasses(),
+ 'containerHtmlAttributes' => $this->getContainerHtmlAttributes(),
+ 'componentId' => $this->getLocalizedComponentId($locale),
+ 'componentClasses' => $this->getComponentClasses(),
+ 'componentHtmlAttributes' => $this->getLocalizedComponentHtmlAttributes($locale),
+ 'validationClass' => fn(
+ ?ViewErrorBag $errors,
+ ?string $locale
+ ) => $this->getLocalizedValidationClass($errors, $locale),
+ 'errorMessage' => fn(
+ ?ViewErrorBag $errors,
+ ?string $locale
+ ) => $this->getLocalizedErrorMessage($errors, $locale),
+ 'successMessage' => fn() => $this->getSuccessMessage(),
+ 'labelPositionedAbove' => $this->getLabelPositionedAbove(),
+ 'label' => $this->getLocalizedLabel($locale),
+ 'type' => $this->getType(),
+ 'name' => $this->getLocalizedName($locale),
+ 'value' => $this->getLocalizedValue($locale),
+ 'placeholder' => $this->getLocalizedPlaceholder($locale),
+ 'prepend' => $this->getLocalizedPrepend($locale),
+ 'append' => $this->getLocalizedAppend($locale),
+ 'caption' => $this->getCaption(),
+ ];
}
- protected function getLocalizedParameters(string $locale): array
+ protected function getLocalizedContainerId(string $locale): ?string
{
- $parentParams = $this->getParameters();
- $componentId = $this->getLocalizedComponentId($locale);
- $containerId = $this->getLocalizedContainerId($locale);
- $componentHtmlAttributes = $this->getLocalizedComponentHtmlAttributes($locale);
- $name = $this->getLocalizedName($locale);
- $prepend = $this->getLocalizedPrepend($locale);
- $append = $this->getLocalizedAppend($locale);
- $label = $this->getLocalizedLabel($locale);
- $value = $this->getLocalizedValue($locale);
- $placeholder = $this->getLocalizedPlaceholder($locale);
- $validationClass = $this->getLocalizedValidationClass($locale);
- $errorMessage = $this->getLocalizedErrorMessage($locale);
-
- return array_merge($parentParams, compact(
- 'componentId',
- 'containerId',
- 'componentHtmlAttributes',
- 'name',
- 'prepend',
- 'append',
- 'label',
- 'value',
- 'placeholder',
- 'validationClass',
- 'errorMessage'
- ));
+ return $this->getContainerId() ? $this->getContainerId() . '-' . $locale : null;
}
protected function getLocalizedComponentId(string $locale): string
@@ -108,35 +108,37 @@ protected function getLocalizedComponentId(string $locale): string
return $this->getComponentId() . '-' . $locale;
}
- protected function getLocalizedContainerId(string $locale): ?string
- {
- return $this->getContainerId() ? $this->getContainerId() . '-' . $locale : null;
- }
-
protected function getLocalizedComponentHtmlAttributes(string $locale): array
{
return array_merge(['data-locale' => $locale], $this->getComponentHtmlAttributes());
}
- protected function getLocalizedName(string $locale): string
+ protected function getLocalizedValidationClass(?ViewErrorBag $errors, string $locale): ?string
{
- return $this->multilingualResolver->resolveLocalizedName($this->getName(), $locale);
- }
-
- protected function getLocalizedPrepend(string $locale): ?string
- {
- $prepend = $this->prepend;
+ if (! $errors) {
+ return null;
+ }
+ if ($errors->isEmpty()) {
+ return null;
+ }
+ if ($errors->has($this->multilingualResolver->resolveErrorMessageBagKey($this->getName(), $locale))) {
+ return $this->getDisplayFailure() ? 'is-invalid' : null;
+ }
- // fallback for usage of closure with multilingual disabled
- return $prepend instanceof Closure ? $prepend($locale) : $prepend;
+ // Only highlight valid fields if there are invalid fields.
+ return $this->getDisplaySuccess() ? 'is-valid' : null;
}
- protected function getLocalizedAppend(string $locale): ?string
+ protected function getLocalizedErrorMessage(?ViewErrorBag $errors, string $locale): ?string
{
- $append = $this->append;
+ if (! $errors) {
+ return null;
+ }
+ if (! $this->getDisplayFailure()) {
+ return null;
+ }
- // fallback for usage of closure with multilingual disabled
- return $append instanceof Closure ? $append($locale) : $append;
+ return $this->multilingualResolver->resolveErrorMessage($this->getName(), $errors, $locale);
}
protected function getLocalizedLabel(string $locale): ?string
@@ -146,18 +148,30 @@ protected function getLocalizedLabel(string $locale): ?string
return $label ? $label . ' (' . mb_strtoupper($locale) . ')' : null;
}
+ protected function getLocalizedName(string $locale): string
+ {
+ return $this->multilingualResolver->resolveLocalizedName($this->getName(), $locale);
+ }
+
protected function getLocalizedValue(string $locale)
{
- $oldValue = $this->multilingualResolver->resolveLocalizedOldValue($this->getName(), $locale);
- /** @var Closure|null $valueClosure */
- $valueClosure = $this->multilingualMode() ? $this->value : null;
- $modelValue = $this->multilingualResolver->resolveLocalizedModelValue(
+ $oldLocalizedValue = $this->multilingualResolver->resolveLocalizedOldValue($this->getName(), $locale);
+ if ($oldLocalizedValue) {
+ return $oldLocalizedValue;
+ }
+ if ($this->value instanceof Closure) {
+ return ($this->value)($locale);
+ }
+ $localizedValue = $this->multilingualResolver->resolveLocalizedModelValue(
$this->getName(),
$locale,
$this->getModel()
);
+ if ($localizedValue) {
+ return $localizedValue;
+ }
- return $oldValue ?? ($valueClosure ? $valueClosure($locale) : $modelValue);
+ return $this->getValue();
}
protected function getLocalizedPlaceholder(string $locale): ?string
@@ -167,21 +181,21 @@ protected function getLocalizedPlaceholder(string $locale): ?string
return $placeholder ? $placeholder . ' (' . mb_strtoupper($locale) . ')' : null;
}
- protected function getLocalizedValidationClass(string $locale): ?string
+ protected function getLocalizedPrepend(string $locale): ?string
{
- if (session()->has('errors')) {
- $errorMessageBagKey = $this->multilingualResolver->resolveErrorMessageBagKey($this->getName(), $locale);
-
- return session()->get('errors')->has($errorMessageBagKey)
- ? ($this->getDisplayFailure() ? 'is-invalid' : null)
- : ($this->getDisplaySuccess() ? 'is-valid' : null);
+ if ($this->prepend instanceof Closure) {
+ return ($this->prepend)($locale);
}
- return null;
+ return $this->getPrepend();
}
- protected function getLocalizedErrorMessage(string $locale): ?string
+ protected function getLocalizedAppend(string $locale): ?string
{
- return $this->multilingualResolver->resolveErrorMessage(parent::getName(), $locale);
+ if ($this->append instanceof Closure) {
+ return ($this->append)($locale);
+ }
+
+ return $this->getAppend();
}
}
diff --git a/src/Components/Form/Abstracts/RadioAbstract.php b/src/Components/Form/Abstracts/RadioAbstract.php
index 5f2253f6..e678ac31 100644
--- a/src/Components/Form/Abstracts/RadioAbstract.php
+++ b/src/Components/Form/Abstracts/RadioAbstract.php
@@ -11,8 +11,11 @@ abstract class RadioAbstract extends CheckableAbstract
protected function getComponentId(): string
{
- return $this->componentId
- ?? $this->getType() . '-' . Str::slug(Str::snake($this->convertArrayNameInNotation('-'), '-')
+ if ($this->componentId) {
+ return $this->componentId;
+ }
+
+ return $this->getType() . '-' . Str::slug(Str::snake($this->convertArrayNameInNotation('-'), '-')
. '-' . Str::snake($this->getValue(), '-'));
}
@@ -22,7 +25,10 @@ protected function getChecked(): bool
if (isset($old) && $old !== '') {
return $old === (string) $this->value;
}
+ if (isset($this->checked)) {
+ return $this->checked;
+ }
- return $this->checked ?? optional($this->model)->{$this->getName()} === $this->value;
+ return optional($this->model)->{$this->getName()} === $this->value;
}
}
diff --git a/src/Components/Form/Abstracts/SelectableAbstract.php b/src/Components/Form/Abstracts/SelectableAbstract.php
index 155ddf52..d0a06405 100644
--- a/src/Components/Form/Abstracts/SelectableAbstract.php
+++ b/src/Components/Form/Abstracts/SelectableAbstract.php
@@ -70,17 +70,14 @@ public function multiple(bool $multiple = true): self
return $this;
}
- protected function getParameters(): array
+ protected function getViewParams(): array
{
- $options = $this->getOptions();
- $optionValueField = $this->getOptionValueField();
- $optionLabelField = $this->getOptionLabelField();
- $multiple = $this->getMultiple();
-
- return array_merge(
- parent::getParameters(),
- compact('options', 'optionValueField', 'optionLabelField', 'multiple')
- );
+ return array_merge(parent::getViewParams(), [
+ 'options' => $this->getOptions(),
+ 'optionValueField' => $this->getOptionValueField(),
+ 'optionLabelField' => $this->getOptionLabelField(),
+ 'multiple' => $this->getMultiple(),
+ ]);
}
protected function getOptions(): array
diff --git a/src/Components/Form/Abstracts/TemporalAbstract.php b/src/Components/Form/Abstracts/TemporalAbstract.php
index 3a067e3d..9c4e7fe4 100644
--- a/src/Components/Form/Abstracts/TemporalAbstract.php
+++ b/src/Components/Form/Abstracts/TemporalAbstract.php
@@ -9,7 +9,7 @@ abstract class TemporalAbstract extends FormAbstract
{
use TemporalValidityChecks;
- protected string $format;
+ private string $format;
public function __construct()
{
diff --git a/src/Components/Form/Abstracts/UploadableAbstract.php b/src/Components/Form/Abstracts/UploadableAbstract.php
index b83adb31..b7832eca 100644
--- a/src/Components/Form/Abstracts/UploadableAbstract.php
+++ b/src/Components/Form/Abstracts/UploadableAbstract.php
@@ -35,12 +35,12 @@ public function showRemoveCheckbox(bool $showRemoveCheckbox = true, ?string $rem
return $this;
}
- protected function getValues(): array
+ protected function getViewParams(): array
{
- return array_merge(parent::getValues(), [
+ return array_merge(parent::getViewParams(), [
'uploadedFileHtml' => $this->getUploadedFileHtml(),
'showRemoveCheckbox' => $this->getShowRemoveCheckbox(),
- 'removeCheckboxLabel' => $this->getRemoveCheckboxLabel(parent::getValues()['label']),
+ 'removeCheckboxLabel' => $this->getRemoveCheckboxLabel(parent::getViewParams()['label']),
'removeCheckboxName' => $this->getShowRemoveCheckboxName(),
]);
}
diff --git a/src/Components/Form/Multilingual/Resolver.php b/src/Components/Form/Multilingual/Resolver.php
index 52de947c..92bd2bc3 100644
--- a/src/Components/Form/Multilingual/Resolver.php
+++ b/src/Components/Form/Multilingual/Resolver.php
@@ -3,6 +3,7 @@
namespace Okipa\LaravelBootstrapComponents\Components\Form\Multilingual;
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\ViewErrorBag;
class Resolver
{
@@ -25,19 +26,19 @@ public function resolveLocalizedOldValue(string $name, string $locale): ?string
public function resolveLocalizedModelValue(string $name, string $locale, ?Model $model): ?string
{
- return data_get(optional($model)->{$name}, $locale);
+ return data_get($model, "$name.$locale");
}
- public function resolveErrorMessage(string $name, string $locale): ?string
+ public function resolveErrorMessage(string $name, ViewErrorBag $errors, string $locale): ?string
{
$errorMessageBagKey = $this->resolveErrorMessageBagKey($name, $locale);
- $errorMessage = optional(session()->get('errors'))->first($errorMessageBagKey);
+ $errorMessage = $errors->first($errorMessageBagKey);
$errorMessage = $this->undoInputNameLaravelUnderscoreRemovalInErrorMessage($name, $locale, $errorMessage);
return $errorMessage
? str_replace(
$errorMessageBagKey,
- ((string) __('validation.attributes.' . $name)) . ' (' . strtoupper($locale) . ')',
+ __('validation.attributes.' . $name) . ' (' . strtoupper($locale) . ')',
$errorMessage
)
: null;
diff --git a/src/Components/Form/Traits/MultilingualValidityChecks.php b/src/Components/Form/Traits/MultilingualValidityChecks.php
deleted file mode 100644
index 3cad83aa..00000000
--- a/src/Components/Form/Traits/MultilingualValidityChecks.php
+++ /dev/null
@@ -1,17 +0,0 @@
-value) && $this->value && $this->multilingualMode()) {
- throw new InvalidArgumentException(get_class($this) . ' : A multilingual component value has to
- be set from this callable result : « ->value(function($locale){}) ».');
- }
- }
-}
diff --git a/src/Components/Form/Traits/RadioValidityChecks.php b/src/Components/Form/Traits/RadioValidityChecks.php
index f10ffec8..c674e9e4 100644
--- a/src/Components/Form/Traits/RadioValidityChecks.php
+++ b/src/Components/Form/Traits/RadioValidityChecks.php
@@ -9,7 +9,8 @@ trait RadioValidityChecks
protected function checkValuesValidity(): void
{
parent::checkValuesValidity();
- if (! isset($this->value) || $this->value === '') {
+ $value = $this->getValue();
+ if (! isset($value) || $value === '') {
throw new InvalidArgumentException(
get_class($this) . ' : Missing $value property. Please use the value() method to set a value.'
);
diff --git a/src/Components/Form/Traits/SelectValidityChecks.php b/src/Components/Form/Traits/SelectValidityChecks.php
index 55a814cf..efc8e7b8 100644
--- a/src/Components/Form/Traits/SelectValidityChecks.php
+++ b/src/Components/Form/Traits/SelectValidityChecks.php
@@ -57,12 +57,12 @@ protected function checkSelectedFieldToCompareExistenceInOption(array $option):
protected function checkMultipleModeModelAttributeType(): void
{
- if ($this->model && isset($this->model->{$this->getName()}) && ! is_array($this->model->{$this->getName()})) {
+ if ($this->getModel() && $this->getModel()->{$this->getName()} && ! is_array($this->getModel()->{$this->getName()})) {
throw new InvalidArgumentException(
get_class($this) . ' : The « ' . $this->getName() . ' » attribute from the given « '
- . $this->model->getMorphClass()
+ . $this->getModel()->getMorphClass()
. ' » model has to be an array when the select() component is in multiple mode : « '
- . gettype($this->model->{$this->getName()}) . ' » type given.'
+ . gettype($this->getModel()->{$this->getName()}) . ' » type given.'
);
}
}
@@ -83,7 +83,7 @@ protected function checkSingleModeSelectedValueToCompareType(): void
if (
$this->selectedValueToCompare
&& ! is_string($this->selectedValueToCompare)
- && ! is_integer($this->selectedValueToCompare)
+ && ! is_int($this->selectedValueToCompare)
) {
throw new InvalidArgumentException(
get_class($this) . ' : Invalid selected() second $valueToCompare argument. '
diff --git a/src/Components/Form/Traits/TemporalValidityChecks.php b/src/Components/Form/Traits/TemporalValidityChecks.php
index 98f0c0d4..bb41410a 100644
--- a/src/Components/Form/Traits/TemporalValidityChecks.php
+++ b/src/Components/Form/Traits/TemporalValidityChecks.php
@@ -17,9 +17,9 @@ protected function checkValuesValidity(): void
protected function checkFormat(): void
{
- if (! $this->format) {
+ if (! $this->getFormat()) {
throw new RuntimeException(get_class($this) . ' : No config or custom format given for the input'
- . ucfirst($this->type) . ' component.');
+ . ucfirst($this->getType()) . ' component.');
}
}
@@ -28,7 +28,7 @@ protected function checkValue(): void
try {
Carbon::parse(parent::getValue());
} catch (Exception $exception) {
- throw new RuntimeException(get_class($this) . ' : The value for the input' . ucfirst($this->type)
+ throw new RuntimeException(get_class($this) . ' : The value for the input' . ucfirst($this->getType())
. ' component must be a valid DateTime object or a formatted string, « ' . parent::getValue()
. ' » given.');
}
diff --git a/src/Components/Media/Abstracts/ImageAbstract.php b/src/Components/Media/Abstracts/ImageAbstract.php
index e05575e0..834ffb96 100644
--- a/src/Components/Media/Abstracts/ImageAbstract.php
+++ b/src/Components/Media/Abstracts/ImageAbstract.php
@@ -83,26 +83,31 @@ public function linkHtmlAttributes(array $linkHtmlAttributes): self
return $this;
}
- protected function getValues(): array
+ protected function getViewParams(): array
{
- $alt = $this->getAlt();
- $width = $this->getWidth();
- $height = $this->getHeight();
- $linkId = $this->getLinkId();
- $linkClasses = $this->getLinkClasses();
- $linkUrl = $this->getLinkUrl();
- $linkTitle = $this->getLinkTitle();
- $linkHtmlAttributes = $this->getLinkHtmlAttributes();
-
- return array_merge(
- parent::getValues(),
- compact('alt', 'width', 'height', 'linkId', 'linkClasses', 'linkUrl', 'linkTitle', 'linkHtmlAttributes')
- );
+ return array_merge(parent::getViewParams(), [
+ 'alt' => $this->getAlt(),
+ 'width' => $this->getWidth(),
+ 'height' => $this->getHeight(),
+ 'linkId' => $this->getLinkId(),
+ 'linkClasses' => $this->getLinkClasses(),
+ 'linkUrl' => $this->getLinkUrl(),
+ 'linkTitle' => $this->getLinkTitle(),
+ 'linkHtmlAttributes' => $this->getLinkHtmlAttributes(),
+ ]);
}
protected function getAlt(): ?string
{
- return $this->alt ?: $this->getLabel() ?: $this->linkTitle;
+ if ($this->alt) {
+ return $this->alt;
+ }
+ $label = $this->getLabel();
+ if ($label) {
+ return $label;
+ }
+
+ return $this->linkTitle;
}
protected function getWidth(): ?int
@@ -134,7 +139,15 @@ protected function getLinkUrl(): ?string
protected function getLinkTitle(): ?string
{
- return $this->linkTitle ?: $this->getLabel() ?: $this->alt;
+ if ($this->linkTitle) {
+ return $this->linkTitle;
+ }
+ $label = $this->getLabel();
+ if ($label) {
+ return $label;
+ }
+
+ return $this->alt;
}
protected function getLinkHtmlAttributes(): array
diff --git a/src/Components/Media/Abstracts/MediaAbstract.php b/src/Components/Media/Abstracts/MediaAbstract.php
index 05009a1e..5901ce82 100644
--- a/src/Components/Media/Abstracts/MediaAbstract.php
+++ b/src/Components/Media/Abstracts/MediaAbstract.php
@@ -39,13 +39,13 @@ public function caption(?string $caption): self
return $this;
}
- protected function getValues(): array
+ protected function getViewParams(): array
{
- $label = $this->getLabel();
- $src = $this->getSrc();
- $caption = $this->getCaption();
-
- return array_merge(parent::getValues(), compact('label', 'src', 'caption'));
+ return array_merge(parent::getViewParams(), [
+ 'label' => $this->getLabel(),
+ 'src' => $this->getSrc(),
+ 'caption' => $this->getCaption(),
+ ]);
}
protected function getLabel(): ?string
diff --git a/src/Components/Media/Abstracts/VideoAbstract.php b/src/Components/Media/Abstracts/VideoAbstract.php
index 9033cf49..9fa6af83 100644
--- a/src/Components/Media/Abstracts/VideoAbstract.php
+++ b/src/Components/Media/Abstracts/VideoAbstract.php
@@ -19,11 +19,9 @@ public function poster(string $poster): self
return $this;
}
- protected function getValues(): array
+ protected function getViewParams(): array
{
- $poster = $this->getPoster();
-
- return array_merge(parent::getValues(), compact('poster'));
+ return array_merge(parent::getViewParams(), ['poster' => $this->getPoster()]);
}
protected function getPoster(): ?string
diff --git a/tests/Unit/Form/Abstracts/InputCheckableTestAbstract.php b/tests/Unit/Form/Abstracts/InputCheckableTestAbstract.php
index 85b267c7..a471cc0c 100644
--- a/tests/Unit/Form/Abstracts/InputCheckableTestAbstract.php
+++ b/tests/Unit/Form/Abstracts/InputCheckableTestAbstract.php
@@ -147,9 +147,7 @@ public function testSetNullValue(): void
public function testSetValueFromClosureWithDisabledMultilingual(): void
{
- $html = $this->getComponent()->name('name')->value(function () {
- return true;
- })->toHtml();
+ $html = $this->getComponent()->name('name')->value(fn() => true)->toHtml();
self::assertStringContainsString('checked="checked', $html);
}
@@ -163,51 +161,36 @@ public function testSetValueOverridesModelValue(): void
{
$user = $this->createUniqueUser();
$html = $this->getComponent()->model($user)->name('active')->value(false)->toHtml();
- self::assertStringContainsString('checked="checked', $html);
+ self::assertStringNotContainsString('checked="checked', $html);
}
public function testOldValue(): void
{
- $oldValue = true;
- $value = false;
$this->app['router']->get('test', [
- 'middleware' => 'web', 'uses' => function () use ($oldValue) {
- $request = request()->merge(['active' => (string) $oldValue]);
- $request->flash();
- },
+ 'middleware' => 'web', 'uses' => fn() => request()->merge(['active' => '1'])->flash(),
]);
$this->call('GET', 'test');
- $html = $this->getComponent()->name('active')->value($value)->toHtml();
+ $html = $this->getComponent()->name('active')->checked(false)->toHtml();
self::assertStringContainsString('checked="checked', $html);
}
public function testOldArrayValue(): void
{
- $oldValue = true;
- $value = false;
$this->app['router']->get('test', [
- 'middleware' => 'web', 'uses' => function () use ($oldValue) {
- $request = request()->merge(['active' => [0 => (string) $oldValue]]);
- $request->flash();
- },
+ 'middleware' => 'web', 'uses' => fn() => request()->merge(['active' => ['1']])->flash(),
]);
$this->call('GET', 'test');
- $html = $this->getComponent()->name('active[0]')->value($value)->toHtml();
+ $html = $this->getComponent()->name('active[0]')->checked(false)->toHtml();
self::assertStringContainsString('checked="checked', $html);
}
public function testOldValueNotChecked(): void
{
- $oldValue = false;
- $value = true;
$this->app['router']->get('test', [
- 'middleware' => 'web', 'uses' => function () use ($oldValue) {
- $request = request()->merge(['active' => (string) $oldValue]);
- $request->flash();
- },
+ 'middleware' => 'web', 'uses' => fn() => request()->merge(['active' => '0'])->flash(),
]);
$this->call('GET', 'test');
- $html = $this->getComponent()->name('active')->value($value)->toHtml();
+ $html = $this->getComponent()->name('active')->checked(true)->toHtml();
self::assertStringNotContainsString('checked="checked', $html);
}
diff --git a/tests/Unit/Form/Abstracts/InputFileTestAbstract.php b/tests/Unit/Form/Abstracts/InputFileTestAbstract.php
index 923688b1..bc59f87f 100644
--- a/tests/Unit/Form/Abstracts/InputFileTestAbstract.php
+++ b/tests/Unit/Form/Abstracts/InputFileTestAbstract.php
@@ -73,38 +73,28 @@ public function testSetValueFromClosureWithDisabledMultilingual(): void
public function testOldValue(): void
{
- $oldValue = 'old-value';
- $value = 'custom-value';
$this->app['router']->get('test', [
- 'middleware' => 'web', 'uses' => function () use ($oldValue) {
- $request = request()->merge(['name' => $oldValue]);
- $request->flash();
- },
+ 'middleware' => 'web', 'uses' => fn() => request()->merge(['name' => 'old-value'])->flash(),
]);
$this->call('GET', 'test');
- $html = $this->getComponent()->name('name')->value($value)->toHtml();
+ $html = $this->getComponent()->name('name')->value('custom-value')->toHtml();
self::assertStringContainsString('
', $html);
+ . '-name">old-value', $html);
self::assertStringNotContainsString('
', $html);
+ . '-name">custom-value', $html);
}
public function testOldArrayValue(): void
{
- $oldValue = 'old-value';
- $value = 'custom-value';
$this->app['router']->get('test', [
- 'middleware' => 'web', 'uses' => function () use ($oldValue) {
- $request = request()->merge(['name' => [0 => $oldValue]]);
- $request->flash();
- },
+ 'middleware' => 'web', 'uses' => fn() => request()->merge(['name' => [0 => 'old-value']])->flash(),
]);
$this->call('GET', 'test');
- $html = $this->getComponent()->name('name[0]')->value($value)->toHtml();
+ $html = $this->getComponent()->name('name[0]')->value('custom-value')->toHtml();
self::assertStringContainsString('
', $html);
+ . '-name-0">old-value', $html);
self::assertStringNotContainsString('
', $html);
+ . '-name-0">custom-value', $html);
}
public function testDefaultPlaceholder(): void
@@ -159,7 +149,6 @@ public function testNoPlaceholderWithNoLabel(): void
. '-name">' . __('No file selected.') . '', $html);
}
-
public function testHidePlaceholder(): void
{
$html = $this->getComponent()->name('name')->placeholder(false)->toHtml();
diff --git a/tests/Unit/Form/Abstracts/InputMultilingualTestAbstract.php b/tests/Unit/Form/Abstracts/InputMultilingualTestAbstract.php
index 99723cc4..39ea4b21 100644
--- a/tests/Unit/Form/Abstracts/InputMultilingualTestAbstract.php
+++ b/tests/Unit/Form/Abstracts/InputMultilingualTestAbstract.php
@@ -3,7 +3,7 @@
namespace Okipa\LaravelBootstrapComponents\Tests\Unit\Form\Abstracts;
use Illuminate\Support\MessageBag;
-use InvalidArgumentException;
+use Illuminate\Support\ViewErrorBag;
use Okipa\LaravelBootstrapComponents\Components\Form\Abstracts\MultilingualAbstract;
use Okipa\LaravelBootstrapComponents\Tests\Dummy\Resolver;
use Okipa\LaravelBootstrapComponents\Tests\Models\User;
@@ -94,14 +94,6 @@ public function testLocalizedModelValueFromCustomMultilingualResolver(): void
}
}
- public function testSetLocalizedWrongValue(): void
- {
- $locales = ['fr', 'en'];
- $value = 'custom-value';
- $this->expectException(InvalidArgumentException::class);
- $this->getComponent()->name('name')->locales($locales)->value($value)->toHtml();
- }
-
public function testSetLocalizedValue(): void
{
$locales = ['fr', 'en'];
@@ -112,9 +104,7 @@ public function testSetLocalizedValue(): void
$html = $this->getComponent()
->name('name')
->locales($locales)
- ->value(function ($locale) use ($values) {
- return $values[$locale];
- })
+ ->value(fn($locale) => $values[$locale])
->toHtml();
foreach ($locales as $locale) {
self::assertStringContainsString(' value="' . $values[$locale] . '"', $html);
@@ -131,10 +121,7 @@ public function testLocalizedOldValue(): void
$values[$locale] = 'custom-value-' . $locale;
}
$this->app['router']->get('test', [
- 'middleware' => 'web', 'uses' => function () use ($oldValues) {
- $request = request()->merge(['name' => $oldValues]);
- $request->flash();
- },
+ 'middleware' => 'web', 'uses' => fn() => request()->merge(['name' => $oldValues])->flash(),
]);
$this->call('GET', 'test');
$html = $this->getComponent()->name('name')->locales($locales)->value(function ($locale) use ($values) {
@@ -160,15 +147,10 @@ public function testLocalizedOldValueFromCustomMultilingualResolver(): void
$values[$locale] = 'custom-value-' . $locale;
}
$this->app['router']->get('test', [
- 'middleware' => 'web', 'uses' => function () use ($oldValues) {
- $request = request()->merge($oldValues);
- $request->flash();
- },
+ 'middleware' => 'web', 'uses' => fn() => request()->merge($oldValues)->flash(),
]);
$this->call('GET', 'test');
- $html = $this->getComponent()->name('name')->value(function ($locale) use ($values) {
- return $values[$locale];
- })->toHtml();
+ $html = $this->getComponent()->name('name')->value(fn($locale) => $values[$locale])->toHtml();
foreach ($resolverLocales as $resolverLocale) {
self::assertStringContainsString('value="' . $oldValues['name_' . $resolverLocale] . '"', $html);
}
@@ -183,9 +165,7 @@ public function testSetLocalizedPrepend(): void
$html = $this->getComponent()
->name('name')
->locales($locales)
- ->prepend(function ($locale) {
- return 'prepend-' . $locale;
- })
+ ->prepend(fn($locale) => 'prepend-' . $locale)
->toHtml();
foreach ($locales as $locale) {
self::assertStringContainsString('
prepend-' . $locale . '', $html);
@@ -259,9 +239,8 @@ public function testSetLocalizedContainerId(): void
public function testLocalizedErrorMessage(): void
{
$locales = ['fr', 'en'];
- $errors = app(MessageBag::class);
- $errors->add('name.fr', 'Dummy name.fr error message.');
- session()->put('errors', $errors);
+ $messageBag = app(MessageBag::class)->add('name.fr', 'Dummy name.fr error message.');
+ $errors = app(ViewErrorBag::class)->put('default', $messageBag);
$html = $this->getComponent()->name('name')->locales($locales)->displayFailure()->render(compact('errors'));
self::assertStringContainsString(
'id="' . $this->getComponentType() . '-name-fr" class="component form-control is-invalid"',
@@ -281,9 +260,8 @@ public function testLocalizedErrorMessage(): void
public function testLocalizedErrorMessageWithSeveralWords(): void
{
$locales = ['fr', 'en'];
- $errors = app(MessageBag::class);
- $errors->add('last_name.fr', 'Dummy last name.fr error message.');
- session()->put('errors', $errors);
+ $messageBag = app(MessageBag::class)->add('last_name.fr', 'Dummy last name.fr error message.');
+ $errors = app(ViewErrorBag::class)->put('default', $messageBag);
$html = $this->getComponent()
->name('last_name')
->locales($locales)
@@ -311,9 +289,8 @@ public function testLocalizedErrorMessageWithSeveralWordsAndCustomMultilingualRe
{
config()->set('bootstrap-components.form.multilingualResolver', Resolver::class);
$locales = ['fr', 'en'];
- $errors = app(MessageBag::class);
- $errors->add('last_name_fr', 'Dummy last name fr error message.');
- session()->put('errors', $errors);
+ $messageBag = app(MessageBag::class)->add('last_name_fr', 'Dummy last name fr error message.');
+ $errors = app(ViewErrorBag::class)->put('default', $messageBag);
$html = $this->getComponent()
->name('last_name')
->locales($locales)
@@ -340,9 +317,8 @@ public function testLocalizedErrorMessageWithSeveralWordsAndCustomMultilingualRe
public function testLocalizedErrorMessageFromCustomMultilingualResolver(): void
{
config()->set('bootstrap-components.form.multilingualResolver', Resolver::class);
- $errors = app(MessageBag::class);
- $errors->add('name_en', 'Dummy name_en error message.');
- session()->put('errors', $errors);
+ $messageBag = app(MessageBag::class)->add('name_en', 'Dummy name_en error message.');
+ $errors = app(ViewErrorBag::class)->put('default', $messageBag);
$html = $this->getComponent()->name('name')->displayFailure()->render(compact('errors'));
self::assertStringContainsString(
'id="' . $this->getComponentType() . '-name-en" class="component form-control is-invalid"',
diff --git a/tests/Unit/Form/Abstracts/InputRadioTestAbstract.php b/tests/Unit/Form/Abstracts/InputRadioTestAbstract.php
index 76ae32bd..4d69a158 100644
--- a/tests/Unit/Form/Abstracts/InputRadioTestAbstract.php
+++ b/tests/Unit/Form/Abstracts/InputRadioTestAbstract.php
@@ -2,7 +2,6 @@
namespace Okipa\LaravelBootstrapComponents\Tests\Unit\Form\Abstracts;
-use RuntimeException;
use InvalidArgumentException;
use Okipa\LaravelBootstrapComponents\Components\Form\Abstracts\RadioAbstract;
@@ -136,55 +135,39 @@ public function testModelValueChecked(): void
public function testOldValue(): void
{
- $oldValue = 'old-value';
$this->app['router']->get('test', [
- 'middleware' => 'web', 'uses' => function () use ($oldValue) {
- $request = request()->merge(['name' => $oldValue]);
- $request->flash();
- },
+ 'middleware' => 'web', 'uses' => fn() => request()->merge(['name' => 'old-value'])->flash(),
]);
$this->call('GET', 'test');
- $html = $this->getComponent()->name('name')->value($oldValue)->checked(false)->toHtml();
+ $html = $this->getComponent()->name('name')->value('old-value')->checked(false)->toHtml();
self::assertStringContainsString('checked="checked', $html);
}
public function testOldZeroValue(): void
{
- $oldValue = 0;
$this->app['router']->get('test', [
- 'middleware' => 'web', 'uses' => function () use ($oldValue) {
- // http values are always stored as string
- $request = request()->merge(['name' => (string) $oldValue]);
- $request->flash();
- },
+ 'middleware' => 'web', 'uses' => fn() => request()->merge(['name' => '0'])->flash(),
]);
$this->call('GET', 'test');
- $html = $this->getComponent()->name('name')->value($oldValue)->checked(false)->toHtml();
+ $html = $this->getComponent()->name('name')->value(0)->checked(false)->toHtml();
self::assertStringContainsString('checked="checked', $html);
}
public function testOldValueNotChecked(): void
{
- $oldValue = 'old-value';
- $value = 'custom-value';
$this->app['router']->get('test', [
- 'middleware' => 'web', 'uses' => function () use ($oldValue) {
- $request = request()->merge(['name' => $oldValue]);
- $request->flash();
- },
+ 'middleware' => 'web', 'uses' => fn() => request()->merge(['name' => 'old-value'])->flash(),
]);
$this->call('GET', 'test');
- $html = $this->getComponent()->name('name')->value($value)->checked()->toHtml();
+ $html = $this->getComponent()->name('name')->value('custom-value')->checked()->toHtml();
self::assertStringNotContainsString('checked="checked', $html);
}
public function testSetLabel(): void
{
- $label = 'custom-label';
- $html = $this->getComponent()->name('name')->label($label)->toHtml();
+ $html = $this->getComponent()->name('name')->label('custom-label')->toHtml();
self::assertStringContainsString(
- '
',
+ '
',
$html
);
}
diff --git a/tests/Unit/Form/Abstracts/InputTestAbstract.php b/tests/Unit/Form/Abstracts/InputTestAbstract.php
index e2ac2591..7b7d08ed 100644
--- a/tests/Unit/Form/Abstracts/InputTestAbstract.php
+++ b/tests/Unit/Form/Abstracts/InputTestAbstract.php
@@ -3,6 +3,7 @@
namespace Okipa\LaravelBootstrapComponents\Tests\Unit\Form\Abstracts;
use Illuminate\Support\MessageBag;
+use Illuminate\Support\ViewErrorBag;
use Okipa\LaravelBootstrapComponents\Components\ComponentAbstract;
use Okipa\LaravelBootstrapComponents\Components\Form\Abstracts\FormAbstract;
use Okipa\LaravelBootstrapComponents\Tests\BootstrapComponentsTestCase;
@@ -223,34 +224,24 @@ public function testSetValueFromClosureWithDisabledMultilingual(): void
public function testOldValue(): void
{
- $oldValue = 'old-value';
- $value = 'custom-value';
$this->app['router']->get('test', [
- 'middleware' => 'web', 'uses' => function () use ($oldValue) {
- $request = request()->merge(['name' => $oldValue]);
- $request->flash();
- },
+ 'middleware' => 'web', 'uses' => fn() => request()->merge(['name' => 'old-value'])->flash(),
]);
$this->call('GET', 'test');
- $html = $this->getComponent()->name('name')->value($value)->toHtml();
- self::assertStringContainsString(' value="' . $oldValue . '"', $html);
- self::assertStringNotContainsString(' value="' . $value . '"', $html);
+ $html = $this->getComponent()->name('name')->value('custom-value')->toHtml();
+ self::assertStringContainsString(' value="old-value"', $html);
+ self::assertStringNotContainsString(' value="custom-value"', $html);
}
public function testOldArrayValue(): void
{
- $oldValue = 'old-value';
- $value = 'custom-value';
$this->app['router']->get('test', [
- 'middleware' => 'web', 'uses' => function () use ($oldValue) {
- $request = request()->merge(['name' => [0 => $oldValue]]);
- $request->flash();
- },
+ 'middleware' => 'web', 'uses' => fn() => request()->merge(['name' => ['old-value']])->flash(),
]);
$this->call('GET', 'test');
- $html = $this->getComponent()->name('name[0]')->value($value)->toHtml();
- self::assertStringContainsString(' value="' . $oldValue . '"', $html);
- self::assertStringNotContainsString(' value="' . $value . '"', $html);
+ $html = $this->getComponent()->name('name[0]')->value('custom-value')->toHtml();
+ self::assertStringContainsString(' value="old-value"', $html);
+ self::assertStringNotContainsString(' value="custom-value"', $html);
}
public function testSetLabel(): void
@@ -368,8 +359,8 @@ public function testSetCustomDisplaySuccess(): void
'bootstrap-components.components.' . $this->getComponentKey(),
get_class($this->getCustomComponent())
);
- $errors = app(MessageBag::class)->add('other_name', 'Dummy error message.');
- session()->put('errors', $errors);
+ $messageBag = app(MessageBag::class)->add('other_name', 'Dummy error message.');
+ $errors = app(ViewErrorBag::class)->put('default', $messageBag);
$html = $this->getComponent()->name('name')->render(compact('errors'));
self::assertStringContainsString('is-valid', $html);
self::assertStringContainsString('