Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 3.3 - Unable to select empty value using EntityType

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!

like image 750
Bedřich Schindler Avatar asked Oct 31 '25 05:10

Bedřich Schindler


1 Answers

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,
]); 
like image 59
habibun Avatar answered Nov 02 '25 20:11

habibun