jeffersongoncalves/filament-cep-field
Filament CEP Field adds a Brazilian postal code (CEP) input to Filament forms with automatic 99999-999 formatting, validation, and address lookup via BrasilAPI/ViaCEP/AwesomeAPI. Includes Laravel model caching, invalidation, queue support, and full Brazilian states mapping.
Installation:
composer require jeffersongoncalves/filament-cep-field
php artisan vendor:publish --tag=cep-migrations
php artisan migrate
Basic Usage:
use JeffersonGoncalves\Filament\CepField\Forms\Components\CepInput;
CepInput::make('cep')
->required()
->label('CEP');
First Use Case:
Add the field to a Filament form (e.g., CreateUserForm or EditUserForm) to enable real-time CEP validation and address auto-fill.
Basic CEP Field:
CepInput::make('postal_code')
->required()
->label('Código Postal');
Auto-Populating Address Fields:
CepInput::make('cep')
->setStreetField('street')
->setNeighborhoodField('neighborhood')
->setCityField('city')
->setStateField('state');
Custom Button Placement:
CepInput::make('cep')
->setMode('prefix') // Places button before input
->setActionLabel('Consultar CEP');
Error Handling:
CepInput::make('cep')
->setErrorMessage('CEP inválido ou não encontrado.');
99999-999).config/cep-field.php.Dynamic Field Mapping:
$fields = ['street', 'neighborhood', 'city', 'state'];
$mappedFields = collect($fields)->mapWithKeys(fn($field) => [$field => $field]);
CepInput::make('cep')
->setStreetField($mappedFields['street'])
->setNeighborhoodField($mappedFields['neighborhood'])
// ... other fields
Conditional Field Population:
Use Filament’s visible() or reactive() methods to show/hide address fields based on CEP input:
TextInput::make('street')
->visible(fn($record) => $record->cep !== null)
->reactive();
Custom API Responses: Override the default API response handling by extending the component:
class CustomCepInput extends CepInput {
protected function handleApiResponse($response) {
// Custom logic for parsing API response
}
}
SSL Certificate Errors:
cacert.pem is configured in php.ini or set via Laravel HTTP client:
Http::withOptions(['verify' => '/path/to/cacert.pem']);
'verify' => false) in production.API Rate Limits:
Field Name Conflicts:
street, city) match the actual field names in your form/model to avoid silent failures.Filament Version Mismatch:
composer.json.API Requests:
'debug' => env('CEP_DEBUG', false),
in config/cep-field.php.Cache Issues:
php artisan cache:clear
php artisan config:clear
Validation Errors:
dehydrateStateUsing or dehydrateState methods override validation logic.Custom API Providers:
CepServiceProvider to add support for additional APIs:
$this->app->bind(CepService::class, function ($app) {
return new CustomCepService(); // Your implementation
});
Override Default Behavior:
php artisan vendor:publish --tag=cep-config
Modify config/cep-field.php to change default API provider, cache settings, etc.Local Testing:
Http::fake([
'https://brasilapi.com.br/api/cep/v1/*' => Http::response(['data' => [...]]),
]);
Cache::tags(['cep'])->put('cep_12345', $data, now()->addHours(1));
How can I help you explore Laravel packages today?