Emoji form field creation and validation in Filament and Laravel

If you need a form field for picking an emoji to save as a resource/model attribute in Filament and Laravel, here’s how I did it:

First, install a few packages:

$ composer require tangodev-it/filament-emoji-picker
$ composer require steppinghat/emoji-detector

Set up your form field:

TextInput::make('emoji')
	->label('Emoji')
	->placeholder('🎉')
	->suffixAction(EmojiPickerAction::make('emoji-title'))
	->rules([
		'nullable',
		fn (): Closure => static function (string $attribute, mixed $value, Closure $fail): void {
			$value = is_string($value) ? trim($value) : $value;

			if ($value === null || $value === '') {
				return;
			}

			$detector = new EmojiDetector;

			if (! $detector->isSingleEmoji((string) $value)) {
				$fail('Please enter exactly one emoji.');
			}
		},
	]),

And then profit! That’s pretty much it.

Things I tried that didn’t work:

  • Setting the max length of the field to 1; as Unicode entities that might span multiple typical characters, this won’t work.
  • Validating the emoji using regular expressions; the Unicode and emoji standards apparently evolve often enough that this will drive you insane.

Thank you to Javan Eskander for php-emoji-detector and Emanuele Gandolfi for filament-emoji-picker.