Skip to content

Commit

Permalink
Refactor WithValidation trait to improve code readability and maintai…
Browse files Browse the repository at this point in the history
…nability
  • Loading branch information
francoism90 committed Sep 19, 2024
1 parent fda50f4 commit 0777adf
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/Forms/Concerns/WithValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function check(): void
rescue(
fn () => $this->validate(),
fn () => $this->reset(),
report: false,
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Support/Html/Elements/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public function message(?string $message = null): static
$element = Span::create();

return $this
->addChild($element->class('text-error-500')->text($message));
->addChild($element->class('label-error')->text($message));
}
}
3 changes: 2 additions & 1 deletion src/Support/Html/Mixins/BaseElementMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use Illuminate\Support\Stringable;
use Spatie\Html\BaseElement;
use stdClass;

class BaseElementMixin
class BaseElementMixin extends stdClass
{
public function crossorigin(): mixed
{
Expand Down
35 changes: 17 additions & 18 deletions src/Support/Html/Mixins/HtmlExtendedMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,26 @@
use Illuminate\Validation\ValidationException;
use Livewire\Form as Livewire;
use Spatie\Html\Elements\Form;
use stdClass;

#[\AllowDynamicProperties]
class HtmlExtendedMixin
class HtmlExtendedMixin extends stdClass
{
protected ?Livewire $form = null;

protected ?MessageBag $errorBag = null;
protected ?MessageBag $messageBag = null;

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

$this->errorBag = null;
$this->messageBag = null;

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

return Form::create()
Expand All @@ -39,30 +40,28 @@ public function closeWireForm(): mixed
return function (): Form {
$this->form = null;

$this->errorBag = null;
$this->messageBag = null;

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

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

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

public function validate(): mixed
public function icon(): mixed
{
return function (string $field, ?string $message = null): Validate {
$hasMessage = $this->errorBag?->has($field);

$message ??= $this->errorBag?->first($field);

return Validate::create()
->classUnless($hasMessage, 'hidden')
->classIfNotNull($hasMessage, 'block py-1 text-sm')
->messageIf($hasMessage, $message);
return function (): Icon {
return Icon::create();
};
}
}
3 changes: 2 additions & 1 deletion src/Support/Html/Mixins/LinkElementMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Foxws\WireUse\Support\Html\Mixins;

use Spatie\Html\Elements\A;
use stdClass;

class LinkElementMixin
class LinkElementMixin extends stdClass
{
public function link(): mixed
{
Expand Down

0 comments on commit 0777adf

Please sign in to comment.