m-adamski/phone-number-bundle

Phone Number Bundle for Symfony

1.0.0 2018-02-01 13:03 UTC

README

Custom PhoneNumber Bundle integrating libphonenumber library into Symfony 4 project. This bundle is inspired by PhoneNumberBundle but simplified and created for own projects - of course you can use it if you want.

Installation

Use Composer to install this bundle into Symfony 4 project. First, edit composer.json file by adding additional repository configuration:

"repositories": [
        {
            "type": "git",
            "url": "https://github.com/m-adamski/symfony-phone-bundle"
        }
    ]

When additional repository is configured it's time to install bundle:

$ composer require m-adamski/symfony-phone-bundle

Configuration

To use this bundle, you need to register it in config/bundles.php - Symfony Flex should do it automatically.

return [
    Adamski\Symfony\PhoneNumberBundle\PhoneNumberBundle::class => ['all' => true],
];

Register new Doctrine Type in config/packages/doctrine.yaml

doctrine:
    dbal:
        types:
            phone_number: Adamski\Symfony\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType

This Bundle provide type template for Bootstrap 4. Register it in config/packages/twig.yaml

twig:
    form_themes:
        - '@PhoneNumber/Form/phone_number_widget.html.twig'

You can also overwrite default Symfony Bootstrap 4 template by adding - '@PhoneNumber/Form/bootstrap_4_layout.html.twig' into form_themes parameter:

twig:
    form_themes:
        - '@PhoneNumber/Form/bootstrap_4_layout.html.twig'
        - '@PhoneNumber/Form/phone_number_widget.html.twig'

How to use it?

Bundle provide additional Doctrine Type and Form Type. First, edit entity to use PhoneNumber Type:

use Adamski\Symfony\PhoneNumberBundle\Model\PhoneNumber;
use Adamski\Symfony\PhoneNumberBundle\Validator\Constraints\PhoneNumber as AssertPhoneNumber;

/**
 * @var string
 * @AssertPhoneNumber
 * @ORM\Column(name="phone_number", type="phone_number", nullable=true)
 */
protected $phoneNumber;

Now it's time to provide changes in Form Type:

use Adamski\Symfony\PhoneNumberBundle\Form\PhoneNumberType;

public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add("phoneNumber", PhoneNumberType::class, [
            "label"     => "Phone number",
            "preferred" => "PL",
            "required"  => false
        ]);
}