phonenumber type for form

This commit is contained in:
Julien Fastré 2022-03-02 15:01:04 +01:00
parent 7bd93e53c2
commit c8c2c4c859
3 changed files with 65 additions and 27 deletions

View File

@ -0,0 +1,53 @@
<?php
namespace Chill\MainBundle\Form\Type;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
use libphonenumber\PhoneNumberType as LibphonenumberPhoneNumberType;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ChillPhoneNumberType extends AbstractType
{
private string $defaultCarrierCode;
private PhoneNumberUtil $phoneNumberUtil;
public function __construct(ParameterBagInterface $parameterBag)
{
$this->defaultCarrierCode = $parameterBag->get('chill_main')['phone_helper']['default_carrier_code'];
$this->phoneNumberUtil = PhoneNumberUtil::getInstance();
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefault('default_region', $this->defaultCarrierCode)
->setDefault('format', PhoneNumberFormat::NATIONAL)
->setDefault('type', \libphonenumber\PhoneNumberType::FIXED_LINE_OR_MOBILE)
->setNormalizer('attr', function(Options $options, $value) {
if (array_key_exists('placeholder', $value)) {
return $value;
}
$examplePhoneNumber = $this->phoneNumberUtil->getExampleNumberForType($this->defaultCarrierCode, $options['type']);
return array_merge(
$value,
[
'placeholder' => PhoneNumberUtil::getInstance()->format($examplePhoneNumber, $options['format']),
]
);
});
}
public function getParent()
{
return PhoneNumberType::class;
}
}

View File

@ -13,12 +13,14 @@ namespace Chill\PersonBundle\Form;
use Chill\MainBundle\Form\Event\CustomizeFormEvent; use Chill\MainBundle\Form\Event\CustomizeFormEvent;
use Chill\MainBundle\Form\Type\ChillDateType; use Chill\MainBundle\Form\Type\ChillDateType;
use Chill\MainBundle\Form\Type\ChillPhoneNumberType;
use Chill\MainBundle\Form\Type\PickCenterType; use Chill\MainBundle\Form\Type\PickCenterType;
use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper; use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Form\Type\GenderType; use Chill\PersonBundle\Form\Type\GenderType;
use Chill\PersonBundle\Form\Type\PersonAltNameType; use Chill\PersonBundle\Form\Type\PersonAltNameType;
use Chill\PersonBundle\Security\Authorization\PersonVoter; use Chill\PersonBundle\Security\Authorization\PersonVoter;
use libphonenumber\PhoneNumberType;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
@ -60,11 +62,13 @@ final class CreationPersonType extends AbstractType
->add('birthdate', ChillDateType::class, [ ->add('birthdate', ChillDateType::class, [
'required' => false, 'required' => false,
]) ])
->add('phonenumber', TelType::class, [ ->add('phonenumber', ChillPhoneNumberType::class, [
'required' => false, 'required' => false,
'type' => PhoneNumberType::FIXED_LINE
]) ])
->add('mobilenumber', TelType::class, [ ->add('mobilenumber', ChillPhoneNumberType::class, [
'required' => false, 'required' => false,
'type' => PhoneNumberType::MOBILE,
]) ])
->add('email', EmailType::class, [ ->add('email', EmailType::class, [
'required' => false, 'required' => false,

View File

@ -14,6 +14,7 @@ namespace Chill\PersonBundle\Form;
use Chill\CustomFieldsBundle\Form\Type\CustomFieldType; use Chill\CustomFieldsBundle\Form\Type\CustomFieldType;
use Chill\MainBundle\Form\Type\ChillCollectionType; use Chill\MainBundle\Form\Type\ChillCollectionType;
use Chill\MainBundle\Form\Type\ChillDateType; use Chill\MainBundle\Form\Type\ChillDateType;
use Chill\MainBundle\Form\Type\ChillPhoneNumberType;
use Chill\MainBundle\Form\Type\ChillTextareaType; use Chill\MainBundle\Form\Type\ChillTextareaType;
use Chill\MainBundle\Form\Type\CommentType; use Chill\MainBundle\Form\Type\CommentType;
use Chill\MainBundle\Form\Type\PickCivilityType; use Chill\MainBundle\Form\Type\PickCivilityType;
@ -135,45 +136,25 @@ class PersonType extends AbstractType
} }
if ('visible' === $this->config['phonenumber']) { if ('visible' === $this->config['phonenumber']) {
$examplePhoneNumber = PhoneNumberUtil::getInstance()
->getExampleNumberForType(
$this->parameterBag->get('chill_main.phone_helper')['default_carrier_code'],
LibphonenumberPhoneNumberType::FIXED_LINE
);
$builder $builder
->add( ->add(
'phonenumber', 'phonenumber',
PhoneNumberType::class, ChillPhoneNumberType::class,
[ [
'default_region' => $this->parameterBag->get('chill_main.phone_helper')['default_carrier_code'],
'format' => PhoneNumberFormat::NATIONAL,
'required' => false, 'required' => false,
'attr' => [ 'type' => \libphonenumber\PhoneNumberType::FIXED_LINE,
'placeholder' => PhoneNumberUtil::getInstance()->format($examplePhoneNumber, PhoneNumberFormat::NATIONAL),
],
] ]
); );
} }
if ('visible' === $this->config['mobilenumber']) { if ('visible' === $this->config['mobilenumber']) {
$examplePhoneNumber = PhoneNumberUtil::getInstance()
->getExampleNumberForType(
$this->parameterBag->get('chill_main.phone_helper')['default_carrier_code'],
LibphonenumberPhoneNumberType::MOBILE
);
$builder $builder
->add( ->add(
'mobilenumber', 'mobilenumber',
PhoneNumberType::class, ChillPhoneNumberType::class,
[ [
'default_region' => $this->parameterBag->get('chill_main.phone_helper')['default_carrier_code'], 'type' => \libphonenumber\PhoneNumberType::MOBILE,
'format' => PhoneNumberFormat::NATIONAL,
'required' => false, 'required' => false,
'attr' => [
'placeholder' => PhoneNumberUtil::getInstance()->format($examplePhoneNumber, PhoneNumberFormat::NATIONAL),
],
] ]
) )
->add('acceptSMS', CheckboxType::class, [ ->add('acceptSMS', CheckboxType::class, [
@ -182,7 +163,7 @@ class PersonType extends AbstractType
} }
$builder->add('otherPhoneNumbers', ChillCollectionType::class, [ $builder->add('otherPhoneNumbers', ChillCollectionType::class, [
'entry_type' => PersonPhoneType::class, 'entry_type' => ChillPhoneNumberType::class,
'button_add_label' => 'Add new phone', 'button_add_label' => 'Add new phone',
'button_remove_label' => 'Remove phone', 'button_remove_label' => 'Remove phone',
'required' => false, 'required' => false,