Friday, 19 October 2018

Symfony3.4 - Pre filled field on POST_SUBMIT Event

I have a form with a contact list. I want the field "first name" appear with the selected contact value after submit. My problem is that the field appear but I cant set the good data, the field always remains empty.

public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder
        ->add('contacts', ChoiceType::class, [
            'label'       => 'Contact',
            'placeholder' => 'Choose a contact',
            'choices'     => $this->getContacts(),
            'mapped'      => false,
        ])
        ->setMethod('POST')
    ;

    $builder->get('contacts')->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {

        $contactId     = $event->getData();
        $parentForm    = $event->getForm()->getParent();

        $contactEntity = $exampleEm->getrepository(Contact::class)->find($contactId);
        $firstName     = $contactEntity->getFirstName();

        // where can I set the 'contactFirstname' data ?

        $parentForm
            ->add('contactFirstname', TextType::class, [
                'label' => 'First name',
            ]);
    })
    ;
}

How to enter the right data so that the field appears pre-filled?



from Symfony3.4 - Pre filled field on POST_SUBMIT Event

No comments:

Post a Comment