tapp/filament-country-code-field
Installation:
composer require tapp/filament-country-code-field:"^2.0"
Ensure your project uses Filament 4.x/5.x (check composer.json for filament/filament version).
Publish Config (optional, for customization):
php artisan vendor:publish --tag="filament-country-code-field-config"
First Use Case:
Add the field to a Filament Resource (e.g., UserResource):
use Tapp\FilamentCountryCodeField\Forms\Components\CountryCodeSelect;
public static function form(Form $form): Form
{
return $form->schema([
CountryCodeSelect::make('country_code'), // Default 2-letter ISO code
]);
}
Verify:
CountryCodeSelect::make('country_code')
->label('Country')
->required()
->columnSpanFull(); // Optional: Full-width on mobile
->searchable() // Enable search
->placeholder('Select a country')
->options(Country::all()->pluck('name', 'code')) // Override default options
use Tapp\FilamentCountryCodeField\Tables\Columns\CountryCodeColumn;
public static function table(Table $table): Table
{
return $table->columns([
CountryCodeColumn::make('country_code')
->label('Country')
->sortable(),
]);
}
use Tapp\FilamentCountryCodeField\Tables\Filters\CountryCodeSelectFilter;
public static function table(Table $table): Table
{
return $table->filters([
CountryCodeSelectFilter::make('country_code')
->label('Filter by Country')
->placeholder('All countries'),
]);
}
CountryCodeSelect::make('country_code')
->rules(['required', 'size:2']) // ISO 2-letter code
pt_BR):
// In config/filament-country-code-field.php
'locale' => app()->getLocale(), // Auto-detect or set manually
CountryCodeSelect::make('address.country_code')
->relationship('address', 'country_code')
// config/filament-country-code-field.php
'styles' => [
'flagSize' => '24px', // Adjust flag icon size
'dropdownWidth' => '300px',
],
country_code VARCHAR(2)).// In User model
protected $casts = [
'country_code' => 'string',
];
$this->get('/admin/resources/users/create')
->assertSee('Country'); // Verify field renders
$this->post('/admin/resources/users', [
'country_code' => 'US',
])->assertRedirect(); // Test save
Version Mismatch:
Class 'CountryCodeSelect' not found.filament/filament and tapp/filament-country-code-field versions align (check compatibility table).Empty State Handling:
country_code is null in the database.->nullable() or provide a default:
CountryCodeSelect::make('country_code')
->default('US')
CSS Registration:
npm run dev or npm run build if customizing assets.Performance:
->options() to limit choices or lazy-load via API.Locale Fallback:
app()->setLocale('fr').'locale' => 'fr' in config/filament-country-code-field.php.Check Rendered HTML:
// In a Filament widget or blade view
{{ dd($this->getTable()->getColumns()) }}
Verify the CountryCodeColumn is registered correctly.
Log Country Data:
// In a Resource's form builder
CountryCodeSelect::make('country_code')
->afterStateUpdated(fn ($state) => \Log::info('Selected:', $state));
Database Schema:
$table->string('country_code', 2)->nullable();
Custom Country Options:
CountryCodeSelect::make('country_code')
->options([
'INTL' => 'International',
'US' => 'United States',
]);
Add Flags Dynamically:
CountryCodeSelect class to fetch flags from an API:
// app/Filament/Extensions/CustomCountryCodeSelect.php
use Tapp\FilamentCountryCodeField\Forms\Components\CountryCodeSelect as BaseSelect;
class CustomCountryCodeSelect extends BaseSelect {
public static function getOptions(): array {
return Cache::remember('custom_country_options', now()->addHours(1), function () {
return Http::get('https://api.example.com/countries')->json();
});
}
}
Filament 5+ Features:
->columnSpanFull() for responsive design:
CountryCodeSelect::make('country_code')
->columnSpanFull()
->extraAttributes(['class' => 'w-full']);
Testing Helpers:
$this->actingAs($user)
->get('/admin/resources/users/create')
->assertSee('🇺🇸 United States'); // Verify flag + name
CountryCodeSelect::make('country_code')
->afterStateUpdated(fn ($state) => $this->dispatchBrowserEvent('update-itinerary'))
public static function getWidgetData(): array {
return [
'country' => CountryCodeColumn::make('country_code')
->state(fn () => 'US'),
];
}
php artisan filament:install if migrating from Filament 3.x.How can I help you explore Laravel packages today?