symfony easyadmin自定义表单生成器

时间:2018-09-13 12:02:23

标签: symfony easyadmin

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

配置表单easyadmin:

form:
    fields:
        - { type: 'group', label: 'Basic Information', icon: 'envelope-o', css_class: 'col-sm-8' }
        - { property: 'title' }
        - { property: 'status' }
        - { type: 'group', label: 'Legal Information', icon: 'briefcase', css_class: 'col-sm-4' }
        - { property: 'pdfFile', type: 'file' }

在AdminController中:

/**
 * @param $entity
 * @param $view
 * @return \Symfony\Component\Form\FormBuilder
 */
protected function createQuotationEntityFormBuilder($entity, $view)
{
    $formBuilder = parent::createEntityFormBuilder($entity, $view);

    $formBuilder->add('customer', EntityType::class, array(
            'class' => 'AppBundle:Person',
            'label' => false,
            'by_reference' => false,
            'query_builder' => function(EntityRepository $er) {
                $user = $this->get('security.token_storage')->getToken()->getUser();
                $query = $er->createQueryBuilder('person')
                    ->leftJoin('person.userCreated', 'user')
                    ->andWhere('person.type = :type')
                    ->setParameter('type', 2) // à changer c'est en dur!
                ;
                if (in_array("ROLE_ADMIN", $user->getRoles())) {
                    $query->andWhere('user.company = :company');
                    $query->setParameter('company', $user->getCompany());
                } else {
                    $query->andWhere('user.id = :user');
                    $query->setParameter('user', $user->getId());
                }

                return $query;
            },
            'multiple' => false,
            'expanded' => false,
            'attr' => ['data-widget' => 'select2'],
            'placeholder' => 'label.form.empty_value'
        )
    );

  return $formBuilder;
}

在我的表单生成器中,我想添加一个字段“ customer”,但它显示在最后,表单的呈现效果不好(参见图片),有人会想到,我可以插入该字段“客户”字段后的状态

Example of form

预先感谢

1 个答案:

答案 0 :(得分:0)

手动渲染:

{{ form_start(form) }}

{{ form_row(form.title) }}
{{ form_row(form.status) }} 
{{ form_row(form.client) }}

...

{{ form_end(form) }}

PS:您在easyadmin配置文件中添加的组选项将被忽略。