F
Filament6mo ago
Glebka

Fill custom edit page form with data

<?php

namespace App\Filament\Resources\LearningCategoryResource\Pages;

use Filament\Forms\Form;
use App\Models\LearningResource;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Pages\EditRecord;
use App\Filament\Resources\LearningCategoryResource;
use Filament\Resources\Pages\Concerns\InteractsWithRecord;

class CustomEditResource extends EditRecord
{
use InteractsWithRecord;

protected static string $resource = LearningCategoryResource::class;

public function mount(int | string $record): void
{
$this->record = LearningResource::findOrFail($record);
}

public function form(Form $form): Form
{
return $form
->columns(12)
->schema([
Section::make('Resource information')
->columnSpan(12)
->columns(12)
->schema([
TextInput::make('name')
->label('Name')
->required()
->columnSpan(10),
]),
]);
}
}
<?php

namespace App\Filament\Resources\LearningCategoryResource\Pages;

use Filament\Forms\Form;
use App\Models\LearningResource;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Pages\EditRecord;
use App\Filament\Resources\LearningCategoryResource;
use Filament\Resources\Pages\Concerns\InteractsWithRecord;

class CustomEditResource extends EditRecord
{
use InteractsWithRecord;

protected static string $resource = LearningCategoryResource::class;

public function mount(int | string $record): void
{
$this->record = LearningResource::findOrFail($record);
}

public function form(Form $form): Form
{
return $form
->columns(12)
->schema([
Section::make('Resource information')
->columnSpan(12)
->columns(12)
->schema([
TextInput::make('name')
->label('Name')
->required()
->columnSpan(10),
]),
]);
}
}
I have created a filament page, selected EditPage when filament asked me what kind of page is this going to be and now I am trying to fill the form with $record data, but I dont understand how to do that. Can someone help? This is a custom edit page for a relationManager, not a resource itself, so in a $resource and a mount function I have a different models.
Solution:
You can access your form using the mount() method. From there, you should be able to fill the fields with your record in this way: ``` public function mount(int | string $record): void {...
Jump to solution
3 Replies
Solution
Alexandre
Alexandre6mo ago
You can access your form using the mount() method. From there, you should be able to fill the fields with your record in this way:
public function mount(int | string $record): void
{
$this->record = LearningResource::findOrFail($record);
$this->fillForm();
}
public function mount(int | string $record): void
{
$this->record = LearningResource::findOrFail($record);
$this->fillForm();
}
Pathros
Pathros5mo ago
How do I make that custom edit page appear in the navigation menu? I mean, if in the class setup I set:
class CustomEditResource extends Page implements HasForms
{
use InteractsWithForms;
class CustomEditResource extends Page implements HasForms
{
use InteractsWithForms;
I can see the page right at the navigation menu. However, if I try, just like in this example, which is what I really want to do (a custom page to edit a record):
class CustomEditResource extends EditRecord
{
use InteractsWithRecord;
class CustomEditResource extends EditRecord
{
use InteractsWithRecord;
the link to the page in the navigation, disappears. So how does this work? How do I set the link to that custom edit page? Could you please shed some light on this?
Codrin
Codrin3mo ago
any update to this?
Want results from more Discord servers?
Add your server