php - Symfony 5.2 EntityType 值对 JSON 对象无效
问题描述
我们使用 Symfony 5.2 作为 Angular 项目的 REST-API,并且在定义 EntityTypes 时无法让 Forms 正常工作:
尝试发送 JSON 对象时出现以下错误:
professionBranch:
ERROR: This value is not valid.
我们的表格:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('professionBranch', EntityType::class, [
'class' => ProfessionBranch::class
])
;
}
我们的请求对象:
{
"idProfession": 1,
"name": "test",
"professionBranch": {
"idProfessionBranch": 1,
"name": "Heilmittel"
}
}
当仅提交 ProfessionBranch 的 id 时,表单会识别该实体并且它可以工作。有没有办法告诉 symfony 在“professionBranch”对象中搜索“idProfessionBranch”键,因为我们不想在前端更改整个表单。
"professionBranch": {
"idProfessionBranch": 1,
"name": "Heilmittel"
}
编辑:这是我使用数据转换器的尝试
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('professionBranch', EntityType::class, [
'class' => ProfessionBranch::class,
]);
$builder->get('professionBranch')
->addModelTransformer($this);
}
public function transform($value)
{
return $value->getIdProfessionBranch();
}
public function reverseTransform($value)
{
}
使用数据转换器返回 ID 时显示以下错误消息:
Argument 1 passed to Symfony\\Bridge\\Doctrine\\Form\\ChoiceList\\IdReader::getIdValue() must be an object or null, int give
解决方案
推荐阅读
- nodes - networkx 算法中是否有一个函数可以返回我指定的某些节点周围的集团/集群/组件?
- c++ - 为什么这个核心转储错误发生在我的课堂上?我已经包含了复制构造函数、复制赋值和析构函数
- gis - 在 NetLogo 中创建网络后隐藏道路 shapefile
- c# - 如何将 C# 中多个表的多个响应设置为单个模型,以便我可以使用 Angular js 'ng-repeat' 获取 html 表?
- javascript - Angular Split() 和 innerHTML
- arrays - 如何使用 Golang 在二维数组中输入?
- javascript - 如何使用javascript将表格中单元格的值设置为文本输入onclick的值?
- docker - 从 Docker Swarm 中其他节点上的服务访问容器
- python - 如何确保 Numeric flask wtforms SelectField 下拉列表中的选定数字多于另一个选择?
- python - 一张图,有两个不同的 y 轴