4

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 ? Commented Nov 23, 2017 at 17:01
  • 1
    It just change a label of a submit button between Add and Edit. Commented Nov 23, 2017 at 17:03

2 Answers 2

4
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,
]); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank youf for response. It is not visible in the snippet above but I already use use Symfony\Bridge\Doctrine\Form\Type\EntityType; in my code. placeholder => ' ... ' only sets a text which is being displayed in the select but I does not affect anything else :/
4

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

1 Comment

what a pain to fix it, both issues are github.com/symfony/symfony/issues/25181 and github.com/symfony/symfony/issues/23961 are closed and apparently related to doctrine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.