我使用symfony 3.4和easycorp / easyadmin-bundle 1.17
这是我的“报价”类,“工匠”字段是“人”实体(person.type = 1)的“注册中”:
class Quotation
{
/** others fields... */
/**
* 1 => 'artisan', 2 => 'customer'
*/
private $type;
/**
* @ORM\ManyToOne(targetEntity="Person", inversedBy="artisanQuotations", cascade= { "persist" })
* @ORM\JoinColumn(name="artisan_id", referencedColumnName="id")
*/
private $artisan;
/** getters and setters ... */
我对使用自定义字段类型的表单字段有疑问
form:
fields:
...
- { property: 'artisan', label: '', type: 'AppBundle\Form\Field\ArtisanType' }
我创建了此表单字段类型,以便能够过滤列表,这要归功于query_builder:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('artisan', EntityType::class, array(
'class' => 'AppBundle:Person',
'label' => false,
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('person')
->where('person.type = 1');
},
'attr' => array('data-widget' => 'select2'),
'multiple' => false,
'expanded'=> false
));
}
我的表单显示得很好,但是当我提交此表单时出现错误:
类型为“ AppBundle \ Entity \ Person”的预期参数,已指定“ array”
预先感谢您的帮助
答案 0 :(得分:0)
应在此处使用configureOptions而不是使用buildForm方法。这样一来,您的表单就不会被其他子表单扩展而导致数组产生错误。
/**
* @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'class' => 'AppBundle:Person',
'label' => false,
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('person')
->where('person.type = 1');
},
'attr' => array('data-widget' => 'select2'),
'multiple' => false,
'expanded'=> false,
]);
}
/**
* @return string|null
*/
public function getParent()
{
return EntityType::class;
}