© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•16mo ago•
6 replies
Eric

Add maxlength to 255 chars on TextInput by default

For all my TextInput fields, I want to set a default maximum length of 255 characters, which is the commonly used VARCHAR length for database fields.

I tried implementing this in
app/Providers/AppServiceProvider.php
app/Providers/AppServiceProvider.php
:
TextInput::configureUsing(function (TextInput $input): void {
    $input->maxLength(255);
});
TextInput::configureUsing(function (TextInput $input): void {
    $input->maxLength(255);
});

However, I encountered an issue: if the TextInput is numeric or decimal, validation fails when the number exceeds 255.

So, I attempted to conditionally apply the maxLength only if the input is not numeric or decimal:

TextInput::configureUsing(function (TextInput $input): void {
    $inputMode = $input->getInputMode();

    if ($inputMode !== 'numeric' && $inputMode !== 'decimal') {
        $input->maxLength(255);
    }
});
TextInput::configureUsing(function (TextInput $input): void {
    $inputMode = $input->getInputMode();

    if ($inputMode !== 'numeric' && $inputMode !== 'decimal') {
        $input->maxLength(255);
    }
});

But this approach doesn’t work because configureUsing is called before the component is used in the code, so inputMode is always empty.

What should i do?
Solution
What you likely need to do is to provide a closure to
maxLength()
maxLength()
and only in that closure call
getInputMode()
getInputMode()
.
You need to imagine that
::configureUsing()
::configureUsing()
is called immediately when someone does
TextInput::make()
TextInput::make()
. So at that point, if you call
getInputMode()
getInputMode()
then the code in the configureUsing does not yet know what other methods are chained after it, so hence it doesn't work (what you also said).

The solution therefore would be to fetch the
getInputMode()
getInputMode()
at run-time, when the getMaxLength() function is actually being called:
TextInput::configureUsing(function (TextInput $component): void {
    $component->maxLength(function (TextInput $component) {
       $inputMode = $component->getInputMode();

       if ($inputMode !== 'numeric' && $inputMode !== 'decimal') {
         return 255;
       }

       return null;
    });
});
TextInput::configureUsing(function (TextInput $component): void {
    $component->maxLength(function (TextInput $component) {
       $inputMode = $component->getInputMode();

       if ($inputMode !== 'numeric' && $inputMode !== 'decimal') {
         return 255;
       }

       return null;
    });
});
Jump to solution
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

->maxLength() not working on textinput
FilamentFFilament / ❓┊help
2y ago
TextInput default = object?
FilamentFFilament / ❓┊help
3y ago
TextInput Default value
FilamentFFilament / ❓┊help
3y ago
TextInput default value on edit page
FilamentFFilament / ❓┊help
2y ago