3
votes

I have a following problem. I have a country form where I use the EntityType to select a contact person from a list of User entities. Symfony correctly renders select with an empty option. Contact person is not required, so the empty option can be selected. So if I select the empty option, I want empty string (sent in request) to be transformed to null. According to the EntityType Symfony documentation It can be done when 'empty_data' => null is set. This works for create form. But If I have already set a contact person in create form and I want to set contact person to null in edit form , null is ignored and value is not changed. Change from user A to user B works fine. Does anybody know why it does not change?

Part of the buildForm method from CountryType.php:

$builder->add('contactPerson', EntityType::class, [
    'class' => 'AppBundle:User',
    'choice_label' => 'username',
    'empty_data' => null,
    'required' => false,
    // 'placeholder' => '' - Can be ommited, empty value is default
]); 

Part of the Country.orm.yml:

 manyToOne:       
    contactPerson:
        targetEntity: AppBundle\Entity\User
        joinColumn:
            name: contact_person
            referencedColumnName: id
            onDelete: RESTRICT
            nullable: true
        fetch: EAGER

editAction method of CountryController.php:

    public function editAction(Request $request, $id)
    {
        /** @var CountryService $countryService */
        $countryService = $this->get('app.country');
        /** @var Country $country */
        $country = $countryService->findById($id);

        if (!$country) {
            $this->addFlash('error', 'back.country.not_found');
            return $this->redirectToRoute('back_country_list');
        }

        $form = $this->createForm(
            CountryType::class,
            $country,
            [
                'edit_action' => true
            ]
        );
        $form->handleRequest($request);

        if ($form->isValid()) {
            $countryService->edit($country);

            $this->addFlash('success', 'back.country.edited');
            return $this->redirectToRoute('back_country_edit', ['id' => $id]);
        }

        return $this->render(
            'AppBundle:back\country:edit.html.twig',
            [
                'form' => $form->createView()
            ]
        );
    }

Content of POST request looks like:

"contactPerson" => ""

But If I enter die(dump($country->getContactPerson()) somewhere after $form->handleRequest($request), value is still set to User, not to null.

Thank you for help!

2
What is the logic change in your type with this option 'edit_action' => true ?Mcsky
It just change a label of a submit button between Add and Edit.Bedřich Schindler

2 Answers

3
votes

It has been big suprise to me but I have probably found a bug in the Symfony. See bug report for more info: https://github.com/symfony/symfony/issues/25181

2
votes
use Symfony\Bridge\Doctrine\Form\Type\EntityType;

use above class and try the following code

$builder->add('contactPerson', EntityType::class, [
    'class' => 'AppBundle:User',
    'choice_label' => 'username',
    'required' => false,
    'placeholder' => 'Please choose a contact person',
    'empty_data' => null,
]);