简体   繁体   中英

symfony easyadmin custom form builder

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM