Symfony3.4 - Pre filled field on POST_SUBMIT Event
Asked Answered
G

3

10

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?

Edit : I found a method, but it's not terrible:

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

('data' => $firstNamedont work for me.)

$parentForm->get('contactFirstname')->setData($firstName); doesn't work either

Glorygloryofthesnow answered 16/10, 2018 at 13:10 Comment(1)
I updated my answer, maybe I hope it will help you.Ommiad
O
1

Can't you simply set the 'data' option of your TextType field?

// ...

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

$parentForm
    ->add('contactFirstname', TextType::class, [
         'label' => 'First name',
         'data' => $firstname //here?
     ]);

EDIT:

According to this post submitted on github, the form field needs to be submitted in order to have it's data changed.

In one of his solutions, he uses the "empty_data" as you did.

In the other one, he adds the field to the builder. Hides it with display: "none"; until the data is submitted.

Ommiad answered 19/10, 2018 at 17:56 Comment(0)
T
1

The docs say

the data of an unmapped field can also be modified directly:

$form->get('agreeTerms')->setData(true);

So try this:

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

$parentForm->get('contactFirstname')->setData($firstName);
Thermochemistry answered 20/10, 2018 at 11:28 Comment(3)
No, the input field remains emptyGlorygloryofthesnow
how about $event->getForm()->get('contactFirstname')->setData($firstName); ?Thermochemistry
No I've tried. Moreover the correct form is rather the parentGlorygloryofthesnow
C
-1

Maybe using a setter before creating your form ?

https://symfony.com/doc/current/forms.html#building-the-form

Chaparral answered 18/10, 2018 at 15:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.