schmeits/filament-character-counter
Filament Character Counter
A character counter for the TextInput, Textarea and RichEditor fields of Filament 4 and 5.

Compatibility
| Plugin version | Filament | Laravel | PHP |
|---|---|---|---|
| 1.x | 3.2 | 10, 11 | 8.1 or newer |
| 5.x | 4, 5 | 12, 13 | 8.2 or newer (8.3 for L13) |
Installation
composer require schmeits/filament-character-counter:"^5.0"
For Filament 3.2, use version ^1.0.
The package ships a small stylesheet that is registered with Filament. After installing or updating, publish Filament's assets as usual:
php artisan filament:assets
Usage
use Schmeits\FilamentCharacterCounter\Forms\Components\RichEditor;
use Schmeits\FilamentCharacterCounter\Forms\Components\Textarea;
use Schmeits\FilamentCharacterCounter\Forms\Components\TextInput;
TextInput::make('title')
->characterLimit(50),
Textarea::make('description')
->characterLimit(155),
RichEditor::make('content')
->characterLimit(155),
Soft limit and hard limit
characterLimit()is a soft limit: it only drives the counter. The user can type past it; the counter then says how many characters are too many, for example "(12 too many)".maxLength()is a hard limit: the browser stops typing at that length and Laravel validates it. Without acharacterLimit(), the counter usesmaxLength().
// Counter shows 0 / 50, typing stops at 50.
TextInput::make('title')
->maxLength(50),
// Counter warns past 150, typing stops at 200.
TextInput::make('title')
->characterLimit(150)
->maxLength(200),
Both accept a closure. Without any limit the counter only shows the number of characters.
Counter inside the field
TextInput and Textarea can show the counter inside the input:
TextInput::make('title')
->showInsideControl()
->characterLimit(50),
Hiding the counter
use Filament\Schemas\Components\Utilities\Get;
// Never show the counter on this field
TextInput::make('slug')
->showCharacterCounter(false)
->characterLimit(100),
// Show it only when another field is on
Textarea::make('bio')
->showCharacterCounter(fn (Get $get): bool => (bool) $get('is_public'))
->characterLimit(255),
How characters are counted
The counter counts per character (code point), the same way Laravel's max: rule does, so an emoji counts as one. It follows typing, pasting, autofill and changes made from PHP, such as $set() or resetting the form.
Accessibility
Going over the limit is shown in words and in bold, not by colour alone, and is announced to screen readers. The input is linked to its counter with aria-describedby.
Configuration
Publish the config file:
php artisan vendor:publish --tag="filament-character-counter-config"
return [
// Set to false to leave out the package stylesheet and style the counter yourself.
'load_css' => true,
];
The counter uses these CSS classes: fi-input-charcounter for the counter and fi-input-charcounter-exceeded when the limit is passed.
Translations
Publish the translations:
php artisan vendor:publish --tag="filament-character-counter-translations"
return [
'character_separator' => ' / ',
'character_label' => 'characters',
'characters_over' => '(:count too many)',
];
Upgrading to 5.2
- The RichEditor is no longer a copy of Filament's view. Filament renders the editor itself and the counter is placed around it, so features such as file attachments and mentions work again. If you published
rich-editor.blade.php, delete it and publish again. - The CSS class for an exceeded limit changed from
warningtofi-input-charcounter-exceeded. - The old translation key
character_seperatoris no longer read. Usecharacter_separator. getShowCharacterCounter()is deprecated in favour ofshouldShowCharacterCounter().
Testing
composer test
composer analyse
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Tally Schmeits
- Inspired by the package by Wiebe Nieuwenhuis
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
Related Packages
Phosphor icons for Filament 4 and 5 as a backed enum that scales with Filament's...
Form builder for Filament panels: build forms in the admin, render them with Bla...
Beautiful, customizable radio and checkbox form layouts for FilamentPHP.
Filament v5 form fields plugin for Laravel — 79 components, virtualized Select w...
TallCMS - A modern CMS for Filament admin panels. Works standalone or as a plugi...
Version History
| Version | Released | PHP | License |
|---|---|---|---|
| v5.2.0 | ^8.2 | MIT | |
| v5.1.0 | ^8.2 | MIT | |
| v5.0.1 | ^8.2 | MIT | |
| v5.0.0 | ^8.2 | MIT | |
| 4.0.0 | ^8.1 | MIT | |
| 1.3.4 | ^8.1 | MIT | |
| 1.3.3 | ^8.1 | MIT | |
| 1.3.2 | ^8.1 | MIT | |
| 1.3.1 | ^8.1 | MIT | |
| 1.3.0 | ^8.1 | MIT | |
| 1.2.4 | ^8.1 | MIT | |
| 1.2.3 | ^8.1 | MIT | |
| 1.2.2 | ^8.1 | MIT | |
| 1.2.1 | ^8.1 | MIT | |
| 1.2.0 | ^8.1 | MIT | |
| 1.1.0 | ^8.1 | MIT | |
| 1.0.0 | ^8.1 | MIT |
Pre-releases (2)
| Version | Released | PHP | License |
|---|---|---|---|
| v4.0.0-beta2 | ^8.1 | MIT | |
| v4.0.0-beta1 | ^8.1 | MIT |