Skip to content

Phone Number🔗

The most advanced library to handle phone numbers is libphonenumber and the corresponding Symfony PhoneNumberBundle.

It allows you to represent phone number as value objects. It handles international phone numbers and it's compatible with all phone number formats.

Installation🔗

1
composer require odolbeau/phone-number-bundle

See documentation for more details.

Parse phone number🔗

1
2
3
4
use libphonenumber\PhoneNumberUtil;

/** @var libphonenumber\PhoneNumber $phoneNumber */
$phoneNumber = PhoneNumberUtil::getInstance()->parse('0324842988');

Doctrine🔗

In order to store phone number value object in your database, the bundle provide a Doctrine type:

1
2
3
4
doctrine:
    dbal:
        types:
            phone_number: Misd\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType

Note: Phone numbers are stored in the international phone number format E.123.

Then you can use this type in your Doctrine mapping:

1
2
3
4
5
6
use libphonenumber\PhoneNumber;

class Foobar
{
    private PhoneNumber $phone;
}
1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="App\Entity\Foobar">
    <field name="phone" type="phone_number" column="phone"/>
</doctrine-mapping>

Form🔗

The bundle provides a form type.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
use libphonenumber\PhoneNumberFormat;
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
use Symfony\Component\Form\FormBuilderInterface;

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('phone', PhoneNumberType::class, [
        'default_region' => 'FR',
        'format' => PhoneNumberFormat::NATIONAL,
    ]);
}

See documentation for more details.

Form type

If you have many phone number input accross your application, I recommend you to create a form extension to set region and format on all inputs at the same time.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
namespace App\Infra\Form\Extension;

use libphonenumber\PhoneNumberFormat;
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\OptionsResolver\OptionsResolver;

class PhoneNumberExtension extends AbstractTypeExtension
{
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'default_region' => 'FR',
            'format' => PhoneNumberFormat::NATIONAL,
        ]);
    }

    public static function getExtendedTypes(): iterable
    {
        return [
            PhoneNumberType::class,
        ];
    }
}

Region and format in the extension can be static like above or depends on your user settings for example.

Twig🔗

The bundle also provide a Twig filter to display phone numbers:

1
{{ foobar.phone|phone_number_format }}

See documentation for other Twig filters.


Last update: December 20, 2024