|
- <?php
-
- namespace Lc\CaracoleBundle\Form\Reduction;
-
- use Symfony\Component\Form\AbstractType;
- use Symfony\Component\Form\Extension\Core\Type\DateType;
- use Symfony\Component\Form\Extension\Core\Type\EmailType;
- use Symfony\Component\Form\Extension\Core\Type\TextType;
- use Symfony\Component\Form\FormBuilderInterface;
- use Symfony\Component\Form\FormEvent;
- use Symfony\Component\Form\FormEvents;
- use Symfony\Component\OptionsResolver\OptionsResolver;
-
- class EditGiftVoucherType extends AbstractType
- {
-
- public function buildForm(FormBuilderInterface $builder, array $options)
- {
- $builder->add('activationDate', DateType::class, [
- 'label' => 'Date d\'activation de votre bon cadeau :',
- 'widget'=> 'single_text',
- 'help'=> 'Date à partir de laquelle la personne pourra utiliser ce bon cadeau.'
- ])
- ->add('title', TextType::class, [
- 'label' => 'Message personnalisé affiché dans le bon cadeau :',
- 'help'=> 'Exemple "Joyeux Noël Tata Suzanne !"'
- ])
- ->add('email', EmailType::class, [
- 'label' => 'Email de la personne à qui vous souhaitez offrir ce bon cadeau :',
- 'help'=> 'Le bon ne sera utilisable qu\'avec cet adresse e-mail'
- ])
- ->add('ownerName', TextType::class,[
- 'label' => 'Offert par :',
- 'help'=> 'Sera affiché dans le bon cadeau'
- ]);
-
- $builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) use ($options) {
- if (isset($options['reduction_gift']) && $options['reduction_gift']) {
- $reductionGift = $options['reduction_gift'];
- $form = $event->getForm();
- if (!$form->isSubmitted()) {
- if ($reductionGift->getActivationDate()) {
- $form->get('activationDate')->setData($reductionGift->getActivationDate());
- }
- if ($reductionGift->getUsers()->count()) {
- $form->get('email')->setData($reductionGift->getUsers()[0]->getEmail());
- }
- if ($reductionGift->getOwnerName()) {
- $form->get('ownerName')->setData($reductionGift->getOwnerName());
- }
- $form->get('title')->setData($reductionGift->getTitle());
- }
- }
- });
- }
-
- public function configureOptions(OptionsResolver $resolver)
- {
- $resolver->setDefaults([
- 'reduction_gift' => null
- ]);
- }
- }
|