F
Filament15mo ago
Yaeger

How to reuse forms?

I have two types of "listings". A create and and an edit page for each. Each form is slightly different. Before I had CreateOrEditListing which handled create/update for both types but I feel like these components quickly get incredibly complicated with a LOT of ugly if statements. Any ideas on how to deal with this?
No description
2 Replies
Yaeger
YaegerOP15mo ago
public function form(Form $form): Form
{
return ListingForm::create($form, withShifts: false);
}
public function form(Form $form): Form
{
return ListingForm::create($form, withShifts: false);
}
Ok I now just extracted everything to a ListingForm class where shared form logic resides with parameters to customise aspects of the form inheritance might also be an option Works alright. Customisation options are provided through methods that can be overridden. Not sure what the best practice is, but I'll stick to this for now.
class CreatePermanentListing extends ListingComponent
{
public function save(ListingService $listingService): void
{
$data = NewPermanentListingDto::from($this->form->getState());

$listing = $listingService->createPermanent($data);

$this->redirectRoute('dashboard.listings.show', ['listing' => $listing]);
}

protected function withShifts(): bool
{
return false;
}

protected function actionText(): string
{
return 'Opdracht plaatsen';
}
}
class CreatePermanentListing extends ListingComponent
{
public function save(ListingService $listingService): void
{
$data = NewPermanentListingDto::from($this->form->getState());

$listing = $listingService->createPermanent($data);

$this->redirectRoute('dashboard.listings.show', ['listing' => $listing]);
}

protected function withShifts(): bool
{
return false;
}

protected function actionText(): string
{
return 'Opdracht plaatsen';
}
}
If anyone has better ideas, let me know pls :)
Patrick Boivin
Patrick Boivin15mo ago
Looks good to me, thanks for sharing that
Want results from more Discord servers?
Add your server