我有一个基于实体的 Symfony 类型 ItemType
。
class IpQuoteItemsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('itemName', TextType::class, [
'label' => 'Produktname'
])
...
->add('specialDiscount', PercentType::class, [
'required' => false,
'label' => 'Sonderrabatt',
'mapped' => false,
'attr' => [
'placeholder' => 'Sonderrabatt 0,00 %'
]
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => IpQuoteItems::class
));
}
}
它在最终形式中用作 CollectionType:
class IpQuotesType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
...
$builder->add('products', CollectionType::class, [
'entry_type' => IpQuoteItemsType::class,
'data' => $items
]);
}
}
在任何情况下我都会收到未映射的字段specialDiscount
。它在 ItemsType
的 PRE_SUBMIT 事件中仍然可用,但在最终表单 QuotesType
中找不到。
是否可以以嵌套形式求和未映射的数据?
最佳答案
您可以像这样在 Controller 中获取未映射的字段:
$form->get('nestedEntity')->get('fieldName')->getData()
我没有使用集合进行测试,但它可以与 OneToOne 关系中的自定义类型一起使用。
希望这有帮助。
关于php - Symfony - 以嵌套形式检索未映射的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60828994/