Skip to content

Commit

Permalink
Refactor HtmlExtendedMixin to improve code readability and maintainab…
Browse files Browse the repository at this point in the history
…ility
  • Loading branch information
francoism90 committed Sep 19, 2024
1 parent 0777adf commit 097bb3a
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions src/Support/Html/Mixins/HtmlExtendedMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use Foxws\WireUse\Support\Html\Elements\Icon;
use Foxws\WireUse\Support\Html\Elements\Validate;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\ValidationException;
use Livewire\Form as Livewire;
use Spatie\Html\Elements\Form;
use stdClass;
Expand All @@ -15,21 +13,11 @@ class HtmlExtendedMixin extends stdClass
{
protected ?Livewire $form = null;

protected ?MessageBag $messageBag = null;

public function wireForm(): mixed
{
return function (Livewire $form, ?string $action = null): Form {
$this->form = $form;

$this->messageBag = null;

try {
$this->form->validate();
} catch (ValidationException $e) {
$this->messageBag = $e->validator->errors();
}

return Form::create()
->attributeIf($action, 'wire:submit', $action);
};
Expand All @@ -40,21 +28,21 @@ public function closeWireForm(): mixed
return function (): Form {
$this->form = null;

$this->messageBag = null;

return Form::create()->close();
};
}

public function error(): mixed
{
return function (string $field, ?string $message = null, ?string $format = null): Validate {
$hasMessage = $this->messageBag?->has($field) ?? false;
$messageBag = $this->form?->getComponent()->getErrorBag();

$hasMessage = $messageBag?->has($field) ?? false;

return Validate::create()
->classUnless($hasMessage, 'hidden')
->classIf($hasMessage, 'label')
->messageIf($hasMessage, $message ?: $this->messageBag?->first($field, $format));
->messageIf($hasMessage, $message ?: $messageBag?->first($field, $format));
};
}

Expand Down

0 comments on commit 097bb3a

Please sign in to comment.