Emoji flag in the Symfony CountryType

Smaine Milianni
2 min readJan 30, 2022

🔥 Emojis are part of our way of communicating, what about adding them to your Symfony form when a user needs to select a country, something like:

It’s easy to create an emoji from the ISO country code, check this comment.

TL;DR: In PHP the code is

Try it here 👀

Now we know how to output a flag from a country code let’s create a new Symfony form type, the EmojiType ✨

1- Create the FormType 🖊

The EmojiTypeshould extend the AbstractType and define the method getParent that returns CountryType to take benefit of the CountryType configuration (options, select…). We also add our helper that transforms a country code into an emoji.

2- Create the choice list 📓

When you create a choice list you can use the choices option BUT according to the documentation you can also use choice_loader option to create a lazy list and we will do this.

3- Use it 🍔

  • Create a form that uses our new form type
  • Call this form from a controller and render it in twig
Smaine Milianni

Fullstack Developer- certified Symfony 4,5 and certified AWS Solution Architect - Freelancer - Remote Worker