symfony easyadmin custom form builder

巧了我就是萌 提交于 2019-12-11 15:00:45

问题


I use symfony 3.4 and easycorp/easyadmin-bundle 1.17

Config form 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' }

In 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;
}

In my form builder I want to add a field "customer" but it appears at the end, the rendering of the form is not good (cf. image), someone would have an idea so that I can insert the field "customer" after the field "status"

Thanks in advance


回答1:


Render it manually:

{{ form_start(form) }}

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

...

{{ form_end(form) }}

PS: the group option that you've added in the easyadmin config file will be ignored.



来源:https://stackoverflow.com/questions/52313338/symfony-easyadmin-custom-form-builder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!