如何解决Symfony 5错误,具有带有一对一关系的嵌入表单
我试图嵌入具有收集类型(一对一关系)的表单,但出现错误:
在属性路径“ patiPatientsSafeData”中给出的“ App \ Entity \ PatientsSafeData”,“数组”类型的预期参数。
在实体“患者”中
<?php
namespace App\Entity;
use App\Repository\PatientsRepository;
use Doctrine\ORM\Mapping as ORM;
class Patients
{
private $id;
private $patiLabel;
/**
* @ORM\OneToOne(targetEntity=PatientsSafeData::class,mappedBy="pasaPatient",cascade={"persist","remove"})
*/
private $patiPatientsSafeData;
以及实体PatientSafeData
<?php
namespace App\Entity;
use App\Repository\PatientsSafeDataRepository;
use Doctrine\ORM\Mapping as ORM;
class PatientsSafeData
{
private $id;
/**
* @ORM\OneToOne(targetEntity=Patients::class,inversedBy="patiPatientsSafeData","remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $pasaPatient;
formType
<?php
namespace App\Form;
use App\Form\PatientsSafeDataType;
use App\Entity\Patients;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class PatientsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder,array $options)
{
$builder
->add('patiLabel',TextType::class,["label" => "Label"])
->add('patiPatientsSafeData',CollectionType::class,[
'entry_type' => PatientsSafeDataType::class,'entry_options' => ['label' => 'Safe Data'],'allow_add' => true
])
模板:
<div class="card-body">
<p>Save Data</p>
<p>
<ul class="safeData" id="safeData" data-prototype="{{ form_widget(form.patiPatientsSafeData.vars.prototype)|e('html_attr') }}">
{% for patSafeData in form.patiPatientsSafeData %}
<li>
{{ form_row(patSafeData.pasaName) }}
{{ form_row(patSafeData.pasaSurname) }}
{{ form_row(patSafeData.pasaDOB) }}
</li>
{% endfor %}
</ul>
</p>
</div>
我得到了错误保存数据。 我不知道我哪里错了, 拜托,有什么主意吗? 预先感谢
解决方法
要解决此问题,您必须为patiPatientSafeData字段创建一个自定义表单类型。 Click here,了解如何创建自定义FormType。 如果不想手动创建表单类型,则可以使用symfony-cli基于PatientSafeData实体创建“子”表单,然后将CollectionType替换为刚创建的新FormType :: class。 /> 您的PatientType表单将如下所示:
public function buildForm(FormBuilderInterface $builder,array $options)
{
$builder
->add('patiLabel',TextType::class,["label" => "Label"])
->add('patiPatientsSafeData',PatientSafeDataType::class)
;
请注意,我已经用您之前应该使用cli创建的新PatientSafeDataType表单替换了CollectionType。
现在您的模板看起来像这样
{{ form_start(patientForm) }}
{{ form_row(patientForm.patiLabel) }}
{{ form_row(patientForm.patiPatientsSafeData) }}
<button type="submit">Submit</button>
{{ form_end(patientForm) }}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。