duan2477 2018-09-12 15:27
浏览 43
已采纳

Symfony 4 TextType required = false获取类型为“string”的预期参数,给出“NULL”

I have a TextType field which is set as below

Type:

        ->add('zipCode',TextType::class, array(
                'attr' => array('maxlength' => 10),
                'required' => false,
                'empty_data' => null
            )
        )

When the data loads from the database the field is null. I just attempt to submit the form and I get

Expected argument of type "string", "NULL" given.

at vendor/symfony/property-access/PropertyAccessor.php:174 at Symfony\Component\PropertyAccess\PropertyAccessor::throwInvalidArgumentException('Argument 1 passed to App\Entity\Patients::setZipCode() must be of the type string, null given,

I'm unsure how to fix this? I WANT the value to be null...? Perhaps it's cause I'm not loading the initial data right on the form?

Related code below, let me know if you need something further:

Twig:

{{ form_widget(view_form.zipCode, { 'attr': {'class': 'mb-2 mb-sm-0 w-100'} }) }}

Controller:

public function view(Request $request)
{
    $patientId = $request->query->get('id');

    if (!empty($patientId)) {
        $search = $this->getDoctrine()
            ->getRepository(Patients::class)
            ->find($patientId);

        if (is_null($search))
            $this->addFlash('danger', 'Invalid Patient');

        $form = $this->createForm(PatientViewType::class,$search);
    }
    else
        $form = $this->createForm(PatientViewType::class);

    dump($request);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $view = $form->getData()();

        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($view);
        $entityManager->flush();

        $this->addFlash('success', 'Patient record updated successfully!');
    }

    return $this->render('patient/view.html.twig', [
        'view_form' => $form->createView()
    ]);
}
  • 写回答

1条回答 默认 最新

  • dstd2129 2018-09-12 15:51
    关注

    I WANT the value to be null...? Perhaps it's cause I'm not loading the initial data right on the form?

    According to the exception message, the method Patients::setZipCode does not accept null values. It would seem that the value is correctly passed as null to your entity, but the Patients class does not accept the value.

    I assume the setZipCode signature looks somewhat like this:

    public function setZipCode(string $zipCode): void
    

    You can make it accept null values by adding a question mark:

    public function setZipCode(?string $zipCode): void
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作