diff --git a/src/Exceptions/FormHasNoFieldsException.php b/src/Exceptions/FormHasNoFieldsException.php new file mode 100644 index 00000000..07ce3c9e --- /dev/null +++ b/src/Exceptions/FormHasNoFieldsException.php @@ -0,0 +1,13 @@ +<?php + +namespace Aerni\LivewireForms\Exceptions; + +use Exception; + +class FormHasNoFieldsException extends Exception +{ + public function __construct(protected string $handle) + { + parent::__construct("The form [{$this->handle}] has no fields."); + } +} diff --git a/src/Livewire/Concerns/WithFields.php b/src/Livewire/Concerns/WithFields.php index 7ac774e5..c6e5ac0c 100644 --- a/src/Livewire/Concerns/WithFields.php +++ b/src/Livewire/Concerns/WithFields.php @@ -2,6 +2,7 @@ namespace Aerni\LivewireForms\Livewire\Concerns; +use Aerni\LivewireForms\Exceptions\FormHasNoFieldsException; use Aerni\LivewireForms\Fields\Captcha; use Aerni\LivewireForms\Fields\Field; use Aerni\LivewireForms\Fields\Honeypot; @@ -45,6 +46,8 @@ protected function fields(): Collection { $honeypot = Honeypot::make(new StatamicField($this->form->honeypot(), [])); + throw_if($this->form->fields()->isEmpty(), new FormHasNoFieldsException($this->handle)); + return $this->form->fields() ->map(fn ($field) => $this->makeFieldFromModel($field)) ->put($honeypot->handle, $honeypot);