PHP code example of kapital-online / filament-form-components

1. Go to this page and download the library: Download kapital-online/filament-form-components library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kapital-online / filament-form-components example snippets


// Önce
use Filament\Forms\Components\Select;

// Sonra
use Kapital\Filament\FormComponents\Components\Select;

// Kullanım tamamen aynı
Select::make('field_name')
    ->options([
        '1' => 'Seçenek 1',
        '2' => 'Seçenek 2',
        '3' => 'Seçenek 3',
    ])
    ->multiple()
    ->disableOptionWhen(fn ($value) => $value === '2')

Select::make('tags')
    ->relationship('tags', 'name')
    ->multiple()
    ->disableOptionWhen(fn ($value) => Tag::find($value)?->is_system ?? false)

Select::make('categories')
    ->options([...])
    ->multiple()
    ->maxSelectable(3) // Maksimum 3 seçim yapılabilir

->maxSelectable(fn () => auth()->user()->isPremium() ? 10 : 3)

Select::make('([...])
    ->multiple()
    ->minSelectable(2) // En az 2 seçim gerekli

->minSelectable(fn () => auth()->user()->isAdmin() ? 0 : 1)

Select::make('permissions')
    ->options([...])
    ->multiple()
    ->selectAllOption() // "Tümünü Seç" butonu ekler
    ->disableOptionWhen(fn($value) => $value === 'dangerous_permission') // Disabled olanlar seçilmez

->selectAllOption(fn () => auth()->user()->isAdmin())

Select::make('filters')
    ->options([...])
    ->multiple()
    ->deselectAllOption() // "Tümünü Temizle" butonu ekler

->deselectAllOption(fn () => auth()->user()->canClearAll())

use Filament\Tables\Table;
use Kapital\Filament\FormComponents\Resources\Table as MasterDetailResourceTable;

public static function table(Table $table): Table
{
    if ($table instanceof MasterDetailResourceTable) {
        $table->masterDetailLivewire(
            component: 'overdue-payment-plan-report-transactions-table',
            parameters: fn (OverduePaymentPlanReport $record): array => [
                'userId' => $record->user_id,
            ],
            key: fn (OverduePaymentPlanReport $record): string => 'overdue-payment-plan-report-' . $record->getKey(),
        );
    }

    return $table
        ->columns([
            // Filament columns...
        ]);
}

use Kapital\Filament\FormComponents\Tables\Concerns\InteractsWithMasterDetailTable;

class ListOverduePaymentPlanReports extends ListRecords
{
    use InteractsWithMasterDetailTable;
}

use Kapital\Filament\FormComponents\Components\Select;
use App\Models\Tag;

Select::make('tags')
    ->label('Sipariş Etiketleri')
    ->relationship('tags', 'name')
    ->multiple()
    ->disableOptionWhen(function ($value) {
        // Sistem etiketlerini devre dışı bırak - kaldırılamazlar
        return Tag::find($value)?->is_system ?? false;
    })
    ->selectAllOption() // "Tümünü Seç" butonu (disabled olanları atlar)
    ->deselectAllOption() // "Tümünü Temizle" butonu
    ->maxSelectable(5) // Sipariş başına maksimum 5 etiket
    ->minSelectable(1) // En az 1 etiket gerekli
    ->searchable()
    ->preload()
    ->helperText('Sistem etiketleri devre dışıdır ve değiştirilemez.')

protected function transformOptionsForJs(array $options): array
{
    return collect($options)
        ->map(fn ($label, $value): array => [
            'label' => $label,
            'value' => strval($value),
            'disabled' => $this->isOptionDisabled($value, $label), // Bu satır eklendi
        ])
        ->values()
        ->all();
}

// Doğru
use Kapital\Filament\FormComponents\Components\Select;

// Yanlış
use Filament\Forms\Components\Select;

// Before
use Filament\Forms\Components\Select;

// After
use Kapital\Filament\FormComponents\Components\Select;

// Usage remains exactly the same
Select::make('field_name')
    ->options([
        '1' => 'Option 1',
        '2' => 'Option 2',
        '3' => 'Option 3',
    ])
    ->multiple()
    ->disableOptionWhen(fn ($value) => $value === '2')

Select::make('tags')
    ->relationship('tags', 'name')
    ->multiple()
    ->disableOptionWhen(fn ($value) => Tag::find($value)?->is_system ?? false)

Select::make('categories')
    ->options([...])
    ->multiple()
    ->maxSelectable(3) // Maximum 3 selections allowed

->maxSelectable(fn () => auth()->user()->isPremium() ? 10 : 3)

Select::make('([...])
    ->multiple()
    ->minSelectable(2) // At least 2 selections 

->minSelectable(fn () => auth()->user()->isAdmin() ? 0 : 1)

Select::make('permissions')
    ->options([...])
    ->multiple()
    ->selectAllOption() // Adds "Select All" button
    ->disableOptionWhen(fn($value) => $value === 'dangerous_permission') // Disabled ones won't be selected

->selectAllOption(fn () => auth()->user()->isAdmin())

Select::make('filters')
    ->options([...])
    ->multiple()
    ->deselectAllOption() // Adds "Clear All" button

->deselectAllOption(fn () => auth()->user()->canClearAll())

use Filament\Tables\Table;
use Kapital\Filament\FormComponents\Resources\Table as MasterDetailResourceTable;

public static function table(Table $table): Table
{
    if ($table instanceof MasterDetailResourceTable) {
        $table->masterDetailLivewire(
            component: 'overdue-payment-plan-report-transactions-table',
            parameters: fn (OverduePaymentPlanReport $record): array => [
                'userId' => $record->user_id,
            ],
            key: fn (OverduePaymentPlanReport $record): string => 'overdue-payment-plan-report-' . $record->getKey(),
        );
    }

    return $table
        ->columns([
            // Filament columns...
        ]);
}

use Kapital\Filament\FormComponents\Tables\Concerns\InteractsWithMasterDetailTable;

class ListOverduePaymentPlanReports extends ListRecords
{
    use InteractsWithMasterDetailTable;
}

use Kapital\Filament\FormComponents\Components\Select;
use App\Models\Tag;

Select::make('tags')
    ->label('Order Tags')
    ->relationship('tags', 'name')
    ->multiple()
    ->disableOptionWhen(function ($value) {
        // Disable system tags - they cannot be removed
        return Tag::find($value)?->is_system ?? false;
    })
    ->selectAllOption() // "Select All" button (skips disabled ones)
    ->deselectAllOption() // "Clear All" button
    ->maxSelectable(5) // Maximum 5 tags per order
    ->minSelectable(1) // At least 1 tag 

protected function transformOptionsForJs(array $options): array
{
    return collect($options)
        ->map(fn ($label, $value): array => [
            'label' => $label,
            'value' => strval($value),
            'disabled' => $this->isOptionDisabled($value, $label), // Added this line
        ])
        ->values()
        ->all();
}

// Correct
use Kapital\Filament\FormComponents\Components\Select;

// Wrong
use Filament\Forms\Components\Select;