F
Filament10mo ago
terumi

How to fill form in a custom page?

hello people. I made a new custom page on a resource with
php artisan make:filament-page ShowTicket --resource=TicketResource
php artisan make:filament-page ShowTicket --resource=TicketResource
Now, I want to display a form for the record I hooked up to the page using:
public function mount(int | string $record): void
{
//$this->fillForms();
$this->record = $this->resolveRecord($record);
}
public function mount(int | string $record): void
{
//$this->fillForms();
$this->record = $this->resolveRecord($record);
}
I made a form and I put it in the blade file. The form is being displayed but is isn't filled. I used fill() on the form but still the form is empty. Anyone knows what's wrong?
4 Replies
LeandroFerreira
LeandroFerreira10mo ago
https://filamentphp.com/docs/3.x/forms/adding-a-form-to-a-livewire-component#adding-the-form
$data = $this->getRecord()->attributesToArray();
$this->form->fill($data);
$data = $this->getRecord()->attributesToArray();
$this->form->fill($data);
terumi
terumi10mo ago
I created a form using the artisan command and it outputs me
Typed property App\Livewire\Tickets\ViewTicket::$record must not be accessed before initialization
Typed property App\Livewire\Tickets\ViewTicket::$record must not be accessed before initialization
on the viewticket livewire component 😦
LeandroFerreira
LeandroFerreira10mo ago
what is the ViewTicket code?
terumi
terumi10mo ago
I think I fixed it, It needed to resolve the model instance and pass it to the form object but now I have another problem: It disregards the relations, I cannot do something like created_by.name, contact_channel.name (which are relations defined in the model). The correct code (apart the relation thing) is this: Pages/ViewTicket:
class ViewTicket extends Page
{
use InteractsWithRecord;

protected static string $resource = TicketResource::class;
protected static string $view = 'filament.resources.ticket-resource.pages.view-ticket';

public function getMaxContentWidth(): ?string
{
return 'full';
}

public function mount(int | string $record): void
{
$this->record = $this->resolveRecord($record);
}
}
class ViewTicket extends Page
{
use InteractsWithRecord;

protected static string $resource = TicketResource::class;
protected static string $view = 'filament.resources.ticket-resource.pages.view-ticket';

public function getMaxContentWidth(): ?string
{
return 'full';
}

public function mount(int | string $record): void
{
$this->record = $this->resolveRecord($record);
}
}
filament/resources/ticket-resource/pages/view-ticket (I resolved the object and passed it here)
<x-filament-panels::page>
@livewire('tickets.view-ticket', ['ticket' => $this->record])
</x-filament-panels::page>
<x-filament-panels::page>
@livewire('tickets.view-ticket', ['ticket' => $this->record])
</x-filament-panels::page>