【问题标题】:Symfony 3.3 - Unable to select empty value using EntityTypeSymfony 3.3 - 无法使用 EntityType 选择空值
【发布时间】:2017-11-23 16:53:41
【问题描述】:

我有以下问题。我有一个国家/地区表单,我使用 EntityTypeUser 实体列表中选择联系人。 Symfony 使用空选项正确呈现选择。联系人不是必需的,因此可以选择空选项。因此,如果我选择空选项,我希望将空字符串(在请求中发送)转换为 null。根据EntityType Symfony documentation 设置'empty_data' => null 时可以做到。这适用于创建表单。但是,如果我已经在 create form 中设置了联系人,并且我想在 edit form 中将联系人设置为 null ,则 null 被忽略并且值不会改变。从用户 A 更改为用户 B 工作正常。有人知道为什么它没有改变吗?

来自CountryType.phpbuildForm方法的一部分:

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

部分Country.orm.yml

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

editAction方法:

    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()
            ]
        );
    }

POST 请求的内容如下:

"contactPerson" => ""

但如果我在$form->handleRequest($request) 之后的某处输入die(dump($country->getContactPerson()),值仍设置为User,而不是null

感谢您的帮助!

【问题讨论】:

  • 使用此选项'edit_action' => true,您的类型的逻辑变化是什么?
  • 它只是在添加和编辑之间更改提交按钮的标签。

标签: php forms symfony symfony-forms


【解决方案1】:

这对我来说是一个很大的惊喜,但我可能在 Symfony 中发现了一个错误。有关更多信息,请参阅错误报告:https://github.com/symfony/symfony/issues/25181

【讨论】:

【解决方案2】:
use Symfony\Bridge\Doctrine\Form\Type\EntityType;

使用上面的类并尝试以下代码

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

【讨论】:

  • 感谢您的回复。它在上面的 sn-p 中不可见,但我已经在我的代码中使用了 use Symfony\Bridge\Doctrine\Form\Type\EntityType;placeholder => ' ... ' 只设置一个正在选择中显示的文本,但我不影响其他任何内容:/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
  • 2021-11-08
  • 2021-11-02
相关资源
最近更新 更多