Member-only story

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
Smaine Milianni

Written by Smaine Milianni

Engineering Manager- Technical Lead - certified Symfony 7,6,5,4 and certified AWS Solution Architect - Remote Worker

Responses (2)

Write a response