FilamentF
Filament11mo ago
Hedi

TextInput, don't count stripped characters?

hello, i have a TextInput that converts the typed text from "12312312312" into "1234 123 12 12" for easier readability. and i set the ->maxLength(14)->minLength(14) at 14, because that's how i can allow the user to actually type the number with the spaces included and i make sure the user writes the entire 11 numbers.

but this always fails, because when ->stripCharacters(' ') is ran, the length becomes 11, and the ->minLength(11) fails. now when i change my code to ->maxLength(11)->minLength(11) this also fails because the field won't allow the user from writing more than 11 characters as the spaces are also counted.

here's my entire code
                    TextInput::make('mobile')
->maxLength(11)
->minLength(11)
->mask(RawJs::make(<<<'JS'

//this code basically converts
xxxxxxxxxxx into xxxx xxx xx xx

let mask = function test(value) {
let val = value.replaceAll(' ', '');
switch (true) {
  case (val.length <= 4):
    return val;
  case (val.length <= 6):
    return val.substr(0, 4) + (val.length > 4 ? ' ' : '') + val.substr(4);
  case (val.length <= 8):
    return val.substr(0, 4) + (val.length > 4 ? ' ' : '') + val.substr(4, 3) + (val.length > 7 ? ' ' : '') + val.substr(7);
  default:
    return val.substr(0, 4) + (val.length > 4 ? ' ' : '') + val.substr(4, 3) + (val.length > 7 ? ' ' : '') + val.substr(7, 2) + (val.length > 9 ? ' ' : '') + val.substr(9);
}
}
$el.value =  mask($input);

JS
))
->stripCharacters(' ')

i apologize for the spaghetti.
if you're reading this and don't understand what i'm trying to do, please at least let me know so i can do a better explanation
Solution
Try placing stripCharachters before max/min. But also.. you could write a custom validation rule which is where the min/max is failing that strips spaces and validates?
Was this page helpful?