src/Form/User/PersonType.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Form\User;
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\Extension\Core\Type\DateType;
  5. use Symfony\Component\Form\Extension\Core\Type\TextType;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7. use Symfony\Component\Validator\Constraints\NotBlank;
  8. class PersonType extends AbstractType
  9. {
  10.     /**
  11.      * @var bool
  12.      */
  13.     protected bool $withCompanyName true;
  14.     /**
  15.      * @var bool
  16.      */
  17.     protected bool $withDayOfBirth true;
  18.     /**
  19.      * @var bool
  20.      */
  21.     protected bool $withPhoneNumber true;
  22.     /**
  23.      * @return bool
  24.      */
  25.     protected function mustAddPhoneNumber(): bool
  26.     {
  27.         return $this->withPhoneNumber;
  28.     }
  29.     /**
  30.      * @return bool
  31.      */
  32.     protected function mustAddCompany(): bool
  33.     {
  34.         return $this->withCompanyName;
  35.     }
  36.     /**
  37.      * @return boolean
  38.      */
  39.     protected function mustAddDayOfBirth(): bool
  40.     {
  41.         return $this->withDayOfBirth;
  42.     }
  43.     /**
  44.      * @return array
  45.      */
  46.     protected function getDateOfBirthOptions(): array
  47.     {
  48.         $year date('Y');
  49.         return [
  50.             'label' => 'Geburtsdatum',
  51.             'years' => range($year 18$year 100)
  52.         ];
  53.     }
  54.     public function buildForm(FormBuilderInterface $builder, array $options)
  55.     {
  56.         $notBlank = new NotBlank();
  57.         $builder->add('first_name'TextType::class, array(
  58.             'label' => 'Vorname',
  59.             'constraints' => $notBlank
  60.         ));
  61.         $builder->add('last_name'TextType::class, array(
  62.             'label' => 'Nachname',
  63.             'constraints' => $notBlank
  64.         ));
  65.         if($this->mustAddCompany()) {
  66.             $builder->add('company_name'TextType::class, array(
  67.                 'required' => false,
  68.                 'label' => 'ggf. Firmenname'
  69.             ));
  70.         }
  71.         if($this->mustAddPhoneNumber()) {
  72.             $builder->add('cell_phone'TextType::class, array(
  73.                 'required' => false,
  74.                 'label' => 'ggf. Telefonnummer'
  75.             ));
  76.         }
  77.         if($this->mustAddDayOfBirth()) {
  78.             $builder->add('date_of_birth'DateType::class, $this->getDateOfBirthOptions());
  79.         }
  80.     }
  81. }