symfony easyadmin 表单字段类型实体与过滤器列表 [英] symfony easyadmin form field type entity with filter list

查看:24
本文介绍了symfony easyadmin 表单字段类型实体与过滤器列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 symfony 3.4 和 easycorp/easyadmin-bundle 1.17

I use symfony 3.4 and easycorp/easyadmin-bundle 1.17

这是我的类报价",工匠"字段是人"实体的注册中"(person.type = 1):

This is my class "Quotation", the "artisan" field is an "under registration" of the "Person" entity (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 ... */

我在使用自定义字段类型的表单字段时遇到问题

I have a problem with a form field using a custom field type

form:
    fields:
        ...
        - { property: 'artisan', label: '', type: 'AppBundle\Form\Field\ArtisanType' }

我创建了这个表单字段类型,以便能够通过 query_builder 过滤列表:

I created this form field type to be able to filter the list thanks to the 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
    ));
}

我的表单显示得很好,但是当我提交此表单时出现错误:

my form is displayed perfectly well but when i submit this form i have an error :

AppBundle\Entity\Person"类型的预期参数,给出数组"

Expected argument of type "AppBundle\Entity\Person", "array" given

预先感谢您的帮助

推荐答案

这里应该使用 configureOptions 来代替 buildForm 方法.这样你的表单就不会被另一个导致数组的子表单扩展.

Instead of using the buildForm method you should use configureOptions here. That way your form is not extended by another subform which results in the array.

use Doctrine\ORM\EntityRepository;

use Symfony\Component\Form\AbstractType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ArtisanType extends AbstractType
{

    /**
    * @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;
    }
}

这篇关于symfony easyadmin 表单字段类型实体与过滤器列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆