src/Form/ContactCeFormType.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Contact;
  4. use Symfony\Component\Form\AbstractType;
  5. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7. use Symfony\Component\OptionsResolver\OptionsResolver;
  8. use Symfony\Component\Form\FormEvent;
  9. use Symfony\Component\Form\FormEvents;
  10. class ContactCeFormType extends AbstractType
  11. {
  12. const PROPERTIES_SHOWED = ['Telephone'];
  13. public function buildForm(FormBuilderInterface $builder, array $options)
  14. {
  15. $builder
  16. ->add('firstName', null, ['attr' => ['class' => 'form-control', 'placeholder' => 'Prénom *']])
  17. ->add('lastName', null, ['attr' => ['class' => 'form-control']])
  18. ->add('email', EmailType::class, ['attr' => ['class' => 'form-control']]);
  19. $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
  20. if ($event->getData() !== null) {
  21. $cpvs = $event->getData()->getContactPropertyValues();
  22. $form = $event->getForm();
  23. foreach ($cpvs as $cpv) {
  24. if (in_array($cpv->getProperty()->getName(), self::PROPERTIES_SHOWED)) {
  25. $form->add($cpv->getProperty()->getName(), ContactPropertyValueType::class, ['data' => $cpv, 'mapped' => false,'attr' => ['class' => 'form-control']]);
  26. }
  27. }
  28. }
  29. });;
  30. }
  31. public function configureOptions(OptionsResolver $resolver)
  32. {
  33. $resolver->setDefaults([
  34. 'data_class' => Contact::class,
  35. ]);
  36. }
  37. }