Skip to content

Commit

Permalink
Merge pull request #47 from square-bit/fix/allow-form-with-relations
Browse files Browse the repository at this point in the history
Allow event's model to have relationship
  • Loading branch information
saade authored Sep 18, 2022
2 parents e8ef3a1 + 51081fb commit b0305dc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/Widgets/Concerns/CanManageEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,17 @@ public function onEventClick($event): void
return;
}

$this->editEventForm
->disabled(! static::canEdit($event))
->fill($event);

if (method_exists($this, 'resolveEventRecord')) {
$this->event = $this->resolveEventRecord($event);
$this->editEventForm->model($this->event);
} else {
$this->event_id = $event['id'] ?? null;
}

$this->editEventForm
->disabled(! static::canEdit($event))
->fill($this->event?->getAttributes() ?? $event);

$this->dispatchBrowserEvent('open-modal', ['id' => 'fullcalendar--edit-event-modal']);
}

Expand Down
11 changes: 9 additions & 2 deletions src/Widgets/Forms/CreateEventForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@
namespace Saade\FilamentFullCalendar\Widgets\Forms;

use Filament\Forms;
use Illuminate\Database\Eloquent\Model;

trait CreateEventForm
{
public $createEventFormState = [];

public function onCreateEventSubmit()
{
$this->createEvent($this->createEventForm->getState());
$eventModel = $this->createEvent($this->createEventForm->getState());
if ($eventModel) {
$this->createEventForm->model($eventModel);
$this->createEventForm->saveRelationships();
}

$this->dispatchBrowserEvent('close-modal', ['id' => 'fullcalendar--create-event-modal']);
}

public function createEvent(array $data): void
public function createEvent(array $data): ?Model
{
// Override this function and do whatever you want with $data
return null;
}

protected static function getCreateEventFormSchema(): array
Expand All @@ -36,6 +42,7 @@ protected function getCreateEventForm(): array
{
return [
'createEventForm' => $this->makeForm()
->model($this->getFormModel()::getModel())
->schema(static::getCreateEventFormSchema())
->statePath('createEventFormState'),
];
Expand Down
1 change: 1 addition & 0 deletions src/Widgets/Forms/EditEventForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected function getEditEventForm(): array
{
return [
'editEventForm' => $this->makeForm()
->model($this->getFormModel())
->schema(static::getEditEventFormSchema())
->statePath('editEventFormState'),
];
Expand Down

0 comments on commit b0305dc

Please sign in to comment.