Skip to content

Commit

Permalink
Throw exception if form has no fields
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni committed May 13, 2024
1 parent 884b4d6 commit cd62dfd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Exceptions/FormHasNoFieldsException.php
Original file line number Diff line number Diff line change
@@ -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.");
}
}
3 changes: 3 additions & 0 deletions src/Livewire/Concerns/WithFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit cd62dfd

Please sign in to comment.