src/Form/Backend/Shared/PersonType.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Form\Backend\Shared;
  3. use App\Entity\Person;
  4. use Symfony\Component\Form\AbstractType;
  5. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7. use Symfony\Component\OptionsResolver\OptionsResolver;
  8. class PersonType extends AbstractType
  9. {
  10.     public const FIELD_SALUTATION 'salutation';
  11.     /**
  12.      * @param FormBuilderInterface $builder
  13.      * @param array                $options
  14.      *
  15.      * @return void
  16.      */
  17.     public function buildForm(FormBuilderInterface $builder, array $options): void
  18.     {
  19.         $builder
  20.             ->add('firstname')
  21.             ->add('lastname');
  22.         $this->addSalutationField($builder);
  23.     }
  24.     /**
  25.      * @param FormBuilderInterface $builder
  26.      *
  27.      * @return void
  28.      */
  29.     protected function addSalutationField(FormBuilderInterface $builder): void
  30.     {
  31.         $builder->add(static::FIELD_SALUTATIONChoiceType::class, [
  32.             'choices' => [
  33.                 'app.person.salutation.salutation_mr' => 'salutation_mr',
  34.                 'app.person.salutation.salutation_ms' => 'salutation_ms',
  35.                 'app.person.salutation.salutation_d' => 'salutation_d',
  36.             ]
  37.         ]);
  38.     }
  39.     /**
  40.      * @param OptionsResolver $resolver
  41.      *
  42.      * @return void
  43.      */
  44.     public function configureOptions(OptionsResolver $resolver): void
  45.     {
  46.         $resolver->setDefaults([
  47.             'data_class' => Person::class,
  48.         ]);
  49.     }
  50. }