From 004655a149ac34413e394055fb5d81fc031a723d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Sun, 8 Aug 2021 19:58:23 +0200 Subject: [PATCH] Allow phone field to be optional --- Form/RegisterType.php | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/Form/RegisterType.php b/Form/RegisterType.php index d34fc0a..56e0add 100644 --- a/Form/RegisterType.php +++ b/Form/RegisterType.php @@ -5,6 +5,7 @@ namespace Rapsys\AirBundle\Form; use Symfony\Component\Form\Extension\Core\Type\TelType; use Symfony\Component\Form\Extension\Core\Type\UrlType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Validator\Constraints\NotBlank; class RegisterType extends \Rapsys\UserBundle\Form\RegisterType { @@ -12,10 +13,33 @@ class RegisterType extends \Rapsys\UserBundle\Form\RegisterType { * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { - return parent::buildForm($builder, $options) - ->add('phone', TelType::class, ['attr' => ['placeholder' => 'Your phone'], 'required' => false]); + //Call parent build form + $form = parent::buildForm($builder, $options); + + //Add extra phone field + if (!empty($options['phone'])) { + $form->add('phone', TelType::class, ['attr' => ['placeholder' => 'Your phone'], 'required' => false]); + } + + //Return form + return $form; + } + + /** + * {@inheritdoc} + */ + public function configureOptions(OptionsResolver $resolver) { + //Call parent configure options + parent::configureOptions($resolver); + + //Set defaults + $resolver->setDefaults(['phone' => true]); + + //Add extra mail option + $resolver->setAllowedTypes('phone', 'boolean'); } + /** * {@inheritdoc} */ -- 2.41.1