<?php
namespace App\Form\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
class PersonType extends AbstractType
{
/**
* @var bool
*/
protected bool $withCompanyName = true;
/**
* @var bool
*/
protected bool $withDayOfBirth = true;
/**
* @var bool
*/
protected bool $withPhoneNumber = true;
/**
* @return bool
*/
protected function mustAddPhoneNumber(): bool
{
return $this->withPhoneNumber;
}
/**
* @return bool
*/
protected function mustAddCompany(): bool
{
return $this->withCompanyName;
}
/**
* @return boolean
*/
protected function mustAddDayOfBirth(): bool
{
return $this->withDayOfBirth;
}
/**
* @return array
*/
protected function getDateOfBirthOptions(): array
{
$year = date('Y');
return [
'label' => 'Geburtsdatum',
'years' => range($year - 18, $year - 100)
];
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$notBlank = new NotBlank();
$builder->add('first_name', TextType::class, array(
'label' => 'Vorname',
'constraints' => $notBlank
));
$builder->add('last_name', TextType::class, array(
'label' => 'Nachname',
'constraints' => $notBlank
));
if($this->mustAddCompany()) {
$builder->add('company_name', TextType::class, array(
'required' => false,
'label' => 'ggf. Firmenname'
));
}
if($this->mustAddPhoneNumber()) {
$builder->add('cell_phone', TextType::class, array(
'required' => false,
'label' => 'ggf. Telefonnummer'
));
}
if($this->mustAddDayOfBirth()) {
$builder->add('date_of_birth', DateType::class, $this->getDateOfBirthOptions());
}
}
}