Skip to content

Instantly share code, notes, and snippets.

@jeffegiovani
Last active September 17, 2025 10:23
Show Gist options
  • Select an option

  • Save jeffegiovani/3554ac062f47d9cb2ff0e8f32c02a801 to your computer and use it in GitHub Desktop.

Select an option

Save jeffegiovani/3554ac062f47d9cb2ff0e8f32c02a801 to your computer and use it in GitHub Desktop.
Character Count - Filament V4 Macro Service Provider
<?php
namespace App\Providers;
use Filament\Forms\Components\Field;
use Filament\Schemas\Components\Html;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Field::macro('characterCounter', function (int $limit = 30) {
$maxLength = $this->getMaxLength() ?: $limit;
return $this
->afterLabel(Html::make(
<<<HTML
<span
x-data="{ get count() { return {$maxLength} - (\$state || '').length } }"
x-text="count"
x-bind:class="count < 0 ? 'text-danger-500' : 'text-success-500'"
class="text-xs"
></span>
HTML
));
});
}
}

[EN] After update any Service Provider of your Application with the macro code, call the macro on your input:

[PT-BR] Adicione a macro em qualquer Service Provider da sua aplicação. Depois basta chamar o método/macro a partir do seu campo de formulário:

TextInput::make('name')->maxLength(120)->characterCounter();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment