Count how many characters left to type in the field
Want to see how many characters left to type in field?
Here are some copy & paste examples:
Textarea::make('content') ->hint(fn ($state, $component) => 'left: ' . $component->getMaxLength() - strlen($state) . ' characters') ->maxlength(10) ->lazy() //or: reactive() for instant update, but less efficient
Textarea::make('sms_content') ->hint(function ($state) { $singleSmsCharactersCount = 160; $charactersCount = strlen($state); $smsCount = 0; if ($charactersCount > 0) { $smsCount = ceil(strlen($state) / $singleSmsCharactersCount); } $leftCharacters = $singleSmsCharactersCount - ($charactersCount % $singleSmsCharactersCount); return $smsCount . ' sms (left: ' . $leftCharacters . ' characters)'; }) ->lazy()
I personally suggest change ->lazy() to ->live() to have the changes appear in real-time
Textarea::make('content') ->hint(fn ($state, $component) => 'left: ' . $component->getMaxLength() - strlen($state) . ' characters') ->maxlength(10) ->live()