更新简单实体时 Symfony 5.0.8 奇怪的问题

Symfony 5.0.8 weird issue when updating a simple entity

这是我的实体:

<?php

namespace App\Entity\Contact;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="contact_contact")
 */
class Contact
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", options={"unsigned":true})
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @Assert\NotBlank
     * @ORM\Column(type="string", length=40, nullable=true)
     */
    private $fname;

    /**
     * @ORM\Column(type="string", length=40, nullable=true)
     */
    private $lname;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getFname(): ?string
    {
        return $this->fname;
    }

    public function setFname(string $fname): self
    {
        $this->fname = $fname;

        return $this;
    }

    public function getLname(): ?string
    {
        return $this->lname;
    }

    public function setLname(?string $lname): self
    {
        $this->lname = $lname;

        return $this;
    }

}

这里是编辑控制器动作代码:

/**
 * @Route("/{id}/edit", name="contact_contact_edit", methods={"GET","POST"})
 */
public function edit(Request $request, Contact $contact): Response
{
    $form = $this->createForm(ContactType::class, $contact);
    $form->handleRequest($request);

    if ($form->isSubmitted()) {
            if ($form->isValid()) {
                $this->getDoctrine()->getManager()->flush();
            }
    }

    return $this->render('contact/contact/edit.html.twig', [
        'contact' => $contact,
        'form' => $form->createView(),
    ]);
}

当我 post 表单但将 fname(名字)字段留空时...我收到此错误 (Symfony\Component\PropertyAccess\Exception\InvalidArgumentException)

Expected argument of type "string", "null" given at property path "fname".

创建实体时,@Assert 按预期工作并且消息是这样说的...但是如果我将其留空并更新 post...bzzzt 错误。

我错过了什么?

编辑 |这是表格 class incase that's doing something?

<?php

namespace App\Form\Contact;

use App\Entity\Contact\Contact;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('fname', TextType::class, ['label' => 'First Name'])
            ->add('lname', TextType::class, ['label' => 'Last Name']);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Contact::class,
            'min_entry' => false,

            // NOTE: Must be added to every form class to disable HTML5 validation
            'attr' => ['novalidate' => 'novalidate']
        ]);

        $resolver->setAllowedTypes('min_entry', 'bool');
    }
}

这就是您应该避免允许表单组件直接更改您的实体的原因之一。它将设置数据,然后对其进行验证。所以完全有可能在提交表单后实体处于无效状态。

无论如何,您可以指定一个空值应该是什么:

->add('fname', TextType::class, ['label' => 'First Name', 'empty_data' => ''])