F
Filament15mo ago
Avriant

How to extract a form object in Filament V2?

Received a great pointer yesterday to here for Custom Pages https://www.youtube.com/watch?v=8Di6Yrqgsl8 Problem is, it's for V3, and I'm trying to finish my old V2 project first before migrating to V3 The dashboard renders, but the form does not 😦 Tried consulting this, but still feel stuck. Any help would be much appreciated! https://livewire.laravel.com/docs/forms#extracting-a-form-object Custom Page CompanyMembers.php
<?php

namespace App\Filament\Pages;

use Filament\Pages\Page;
use Filament\Resources\Form;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Contracts\Hasforms;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;

class CompanyMembers extends Page implements Hasforms
{
use InteractsWithForms;

public ?array $data = [];

protected static ?string $model = CompanyMembers::class;

protected static ?string $navigationIcon = 'heroicon-o-user';

protected static ?string $modelLabel = '社員数ブレイクダウン';

protected static string $view = 'filament.pages.company-members';

public function mount(): void {
$this->form->fill();
}

public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('total')->placeholder('10人')->label('トータル'),
TextInput::make('members-20s')->placeholder('2人')->label('20代'),
TextInput::make('members-30s')->placeholder('0人')->label('30代'),
TextInput::make('members-40s')->placeholder('4人')->label('40代'),
TextInput::make('members-50s')->placeholder('3人')->label('50代'),
TextInput::make('members-60s')->placeholder('1人')->label('60代'),
Toggle::make('daiku')->label('専属大工(あり・なし)'),
])
->statePath('data');
}
}
<?php

namespace App\Filament\Pages;

use Filament\Pages\Page;
use Filament\Resources\Form;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Contracts\Hasforms;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;

class CompanyMembers extends Page implements Hasforms
{
use InteractsWithForms;

public ?array $data = [];

protected static ?string $model = CompanyMembers::class;

protected static ?string $navigationIcon = 'heroicon-o-user';

protected static ?string $modelLabel = '社員数ブレイクダウン';

protected static string $view = 'filament.pages.company-members';

public function mount(): void {
$this->form->fill();
}

public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('total')->placeholder('10人')->label('トータル'),
TextInput::make('members-20s')->placeholder('2人')->label('20代'),
TextInput::make('members-30s')->placeholder('0人')->label('30代'),
TextInput::make('members-40s')->placeholder('4人')->label('40代'),
TextInput::make('members-50s')->placeholder('3人')->label('50代'),
TextInput::make('members-60s')->placeholder('1人')->label('60代'),
Toggle::make('daiku')->label('専属大工(あり・なし)'),
])
->statePath('data');
}
}
Blade (adjusted x-filament for V2)
<x-filament::page>
<x-filament::form>
{{ $this->form }}
</x-filament::form>
</x-filament::page>
<x-filament::page>
<x-filament::form>
{{ $this->form }}
</x-filament::form>
</x-filament::page>
Filament Daily
YouTube
Filament: Only Edit Form with Custom Page and Livewire
If you want to use a Filament form outside of the Resource, here's one example. Original tutorial. Filament: Edit Only Single Record with Custom Page https://laraveldaily.com/post/filament-edit-only-single-record-custom-page
Laravel
Forms | Laravel
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
7 Replies
krekas
krekas15mo ago
you can't use form objects with v2 you said it yourself
Fally
Fally15mo ago
The way I did it even when it's just one form (my needs were to add multiple forms on a page) is this:
protected function getForms(): array
{
return [
'form' => $this->makeForm()
->schema($this->getFormSchema())
];
}
protected function getFormSchema(): array
{
return [ TextInput::make('startNumber')];
}
protected function getForms(): array
{
return [
'form' => $this->makeForm()
->schema($this->getFormSchema())
];
}
protected function getFormSchema(): array
{
return [ TextInput::make('startNumber')];
}
The keys you have in the getForms array are what you use in the blade file to display them. $this->form in this case. This is my blade file:
<form wire:submit.prevent="save">
{{ $this->form }}
<x-filament::button type="submit" class="mt-5" wire:loading.attr="disabled">Download</x-filament::button>
</form>
<form wire:submit.prevent="save">
{{ $this->form }}
<x-filament::button type="submit" class="mt-5" wire:loading.attr="disabled">Download</x-filament::button>
</form>
Dennis Koch
Dennis Koch15mo ago
You are mixing Livewire forms with Filament Form Builder. Please refer the Filament docs and not to Livewire docs.
Avriant
AvriantOP15mo ago
Got something urgent right now, will test this tomorrow first thing in the morning! Thanks!
Avriant
AvriantOP15mo ago
That's precisely what I did, but it was very hard for me to translate V2 Filament Documentation for custom pages into what this guy does here https://www.youtube.com/watch?v=8Di6Yrqgsl8 ...and I ended up trying to imitate what he does with my little knowledge only to fail 🙇‍♂️
Filament Daily
YouTube
Filament: Only Edit Form with Custom Page and Livewire
If you want to use a Filament form outside of the Resource, here's one example. Original tutorial. Filament: Edit Only Single Record with Custom Page https://laraveldaily.com/post/filament-edit-only-single-record-custom-page
Dennis Koch
Dennis Koch15mo ago
Your not doing yourself when you apply stuff from v3 videos to v2. Just follow the form builder docs.
Avriant
AvriantOP15mo ago
I'll try to conquer it again using only v2 docs 🐘 This works perfectly, but I'll try to do the same thing with V2 docs (only edit route, single entry (no table generated)) Okay so... What if I need to have only 1 page that creates entry or edits it, no intermediary table pages on Filament V2 (the video does it successfully on V3)? My original idea was to grab the logic from /edit route and modify it, but this did not work (the /edit route is simply empty and not populated)...
'index' => Pages\ListCompanyMembers::route('/'),
'create' => Pages\CreateCompanyMembers::route('/create'),
**'edit' => Pages\CompanyMembers::route('/edit'),**
'index' => Pages\ListCompanyMembers::route('/'),
'create' => Pages\CreateCompanyMembers::route('/create'),
**'edit' => Pages\CompanyMembers::route('/edit'),**
CompanyMembers.php (Custom Page)
<?php

namespace App\Filament\Resources\CompanyMembersResource\Pages;

use Filament\Resources\Form;
use Filament\Resources\Pages\Page;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Components\TextInput;
use App\Filament\Resources\CompanyMembersResource;

class CompanyMembersEdit extends Page
{
protected static string $resource = CompanyMembersResource::class;

protected static string $view = 'filament.resources.company-members-resource.pages.company-members-edit';

protected static ?string $model = CompanyMembers::class;

public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('total')->placeholder('10人')->label('トータル'),
TextInput::make('members-20s')->placeholder('2人')->label('20代'),
TextInput::make('members-30s')->placeholder('0人')->label('30代'),
TextInput::make('members-40s')->placeholder('4人')->label('40代'),
TextInput::make('members-50s')->placeholder('3人')->label('50代'),
TextInput::make('members-60s')->placeholder('1人')->label('60代'),
Toggle::make('daiku')->label('専属大工(あり・なし)'),
]);
}
}
<?php

namespace App\Filament\Resources\CompanyMembersResource\Pages;

use Filament\Resources\Form;
use Filament\Resources\Pages\Page;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Components\TextInput;
use App\Filament\Resources\CompanyMembersResource;

class CompanyMembersEdit extends Page
{
protected static string $resource = CompanyMembersResource::class;

protected static string $view = 'filament.resources.company-members-resource.pages.company-members-edit';

protected static ?string $model = CompanyMembers::class;

public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('total')->placeholder('10人')->label('トータル'),
TextInput::make('members-20s')->placeholder('2人')->label('20代'),
TextInput::make('members-30s')->placeholder('0人')->label('30代'),
TextInput::make('members-40s')->placeholder('4人')->label('40代'),
TextInput::make('members-50s')->placeholder('3人')->label('50代'),
TextInput::make('members-60s')->placeholder('1人')->label('60代'),
Toggle::make('daiku')->label('専属大工(あり・なし)'),
]);
}
}
Want results from more Discord servers?
Add your server