diff --git a/src/Bundle/ChillMainBundle/Form/Type/ChillPhoneNumberType.php b/src/Bundle/ChillMainBundle/Form/Type/ChillPhoneNumberType.php new file mode 100644 index 000000000..9edd7aca4 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Form/Type/ChillPhoneNumberType.php @@ -0,0 +1,53 @@ +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; + } + +} diff --git a/src/Bundle/ChillPersonBundle/Form/CreationPersonType.php b/src/Bundle/ChillPersonBundle/Form/CreationPersonType.php index a33858c02..3aa8e3b15 100644 --- a/src/Bundle/ChillPersonBundle/Form/CreationPersonType.php +++ b/src/Bundle/ChillPersonBundle/Form/CreationPersonType.php @@ -13,12 +13,14 @@ namespace Chill\PersonBundle\Form; use Chill\MainBundle\Form\Event\CustomizeFormEvent; use Chill\MainBundle\Form\Type\ChillDateType; +use Chill\MainBundle\Form\Type\ChillPhoneNumberType; use Chill\MainBundle\Form\Type\PickCenterType; use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Form\Type\GenderType; use Chill\PersonBundle\Form\Type\PersonAltNameType; use Chill\PersonBundle\Security\Authorization\PersonVoter; +use libphonenumber\PhoneNumberType; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Form\AbstractType; @@ -60,11 +62,13 @@ final class CreationPersonType extends AbstractType ->add('birthdate', ChillDateType::class, [ 'required' => false, ]) - ->add('phonenumber', TelType::class, [ + ->add('phonenumber', ChillPhoneNumberType::class, [ 'required' => false, + 'type' => PhoneNumberType::FIXED_LINE ]) - ->add('mobilenumber', TelType::class, [ + ->add('mobilenumber', ChillPhoneNumberType::class, [ 'required' => false, + 'type' => PhoneNumberType::MOBILE, ]) ->add('email', EmailType::class, [ 'required' => false, diff --git a/src/Bundle/ChillPersonBundle/Form/PersonType.php b/src/Bundle/ChillPersonBundle/Form/PersonType.php index 804757986..9ba621633 100644 --- a/src/Bundle/ChillPersonBundle/Form/PersonType.php +++ b/src/Bundle/ChillPersonBundle/Form/PersonType.php @@ -14,6 +14,7 @@ namespace Chill\PersonBundle\Form; use Chill\CustomFieldsBundle\Form\Type\CustomFieldType; use Chill\MainBundle\Form\Type\ChillCollectionType; use Chill\MainBundle\Form\Type\ChillDateType; +use Chill\MainBundle\Form\Type\ChillPhoneNumberType; use Chill\MainBundle\Form\Type\ChillTextareaType; use Chill\MainBundle\Form\Type\CommentType; use Chill\MainBundle\Form\Type\PickCivilityType; @@ -135,45 +136,25 @@ class PersonType extends AbstractType } if ('visible' === $this->config['phonenumber']) { - $examplePhoneNumber = PhoneNumberUtil::getInstance() - ->getExampleNumberForType( - $this->parameterBag->get('chill_main.phone_helper')['default_carrier_code'], - LibphonenumberPhoneNumberType::FIXED_LINE - ); - $builder ->add( 'phonenumber', - PhoneNumberType::class, + ChillPhoneNumberType::class, [ - 'default_region' => $this->parameterBag->get('chill_main.phone_helper')['default_carrier_code'], - 'format' => PhoneNumberFormat::NATIONAL, 'required' => false, - 'attr' => [ - 'placeholder' => PhoneNumberUtil::getInstance()->format($examplePhoneNumber, PhoneNumberFormat::NATIONAL), - ], + 'type' => \libphonenumber\PhoneNumberType::FIXED_LINE, ] ); } if ('visible' === $this->config['mobilenumber']) { - $examplePhoneNumber = PhoneNumberUtil::getInstance() - ->getExampleNumberForType( - $this->parameterBag->get('chill_main.phone_helper')['default_carrier_code'], - LibphonenumberPhoneNumberType::MOBILE - ); - $builder ->add( 'mobilenumber', - PhoneNumberType::class, + ChillPhoneNumberType::class, [ - 'default_region' => $this->parameterBag->get('chill_main.phone_helper')['default_carrier_code'], - 'format' => PhoneNumberFormat::NATIONAL, + 'type' => \libphonenumber\PhoneNumberType::MOBILE, 'required' => false, - 'attr' => [ - 'placeholder' => PhoneNumberUtil::getInstance()->format($examplePhoneNumber, PhoneNumberFormat::NATIONAL), - ], ] ) ->add('acceptSMS', CheckboxType::class, [ @@ -182,7 +163,7 @@ class PersonType extends AbstractType } $builder->add('otherPhoneNumbers', ChillCollectionType::class, [ - 'entry_type' => PersonPhoneType::class, + 'entry_type' => ChillPhoneNumberType::class, 'button_add_label' => 'Add new phone', 'button_remove_label' => 'Remove phone', 'required' => false,