/**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $preSetDataListener = function (FormEvent $event) use($options) {
         $form = $event->getForm();
         $data = $event->getData() ?: array();
         // modify some of the settings inherited from the parent form type
         $options['compound'] = false;
         // normalize choices list
         $options['choices'] = is_array($data) || $data instanceof \Traversable ? $data : array($data);
         // create autocomplete form field
         $form->add('autocomplete', LegacyFormHelper::getType('entity'), $options);
     };
     $preSubmitListener = function (FormEvent $event) {
         $form = $event->getForm();
         if (null === ($data = $event->getData())) {
             $data = array('autocomplete' => array());
             $event->setData($data);
         }
         // reuse autocomplete options, but replace initial choices with submitted data
         $options = $form->get('autocomplete')->getConfig()->getOptions();
         $options['choices'] = $options['em']->getRepository($options['class'])->findBy(array($options['id_reader']->getIdField() => $data['autocomplete']));
         if (isset($options['choice_list'])) {
             // clear choice list for SF < 3.0
             $options['choice_list'] = null;
         }
         // reset autocomplete form field with new choices list
         $form->add('autocomplete', LegacyFormHelper::getType('entity'), $options);
     };
     $builder->addEventListener(FormEvents::PRE_SET_DATA, $preSetDataListener)->addEventListener(FormEvents::PRE_SUBMIT, $preSubmitListener)->addModelTransformer(new CallbackTransformer(function ($entity) {
         return array('autocomplete' => $entity);
     }, function (array $compound) {
         return $compound['autocomplete'];
     }));
 }
 /**
  * @dataProvider shortTypesToFqcnProvider
  */
 public function testGetType($shortType, $expected)
 {
     if (LegacyFormHelper::useLegacyFormComponent()) {
         $expected = $shortType;
     }
     $this->assertSame($expected, LegacyFormHelper::getType($shortType));
 }
 public function preSubmit(FormEvent $event)
 {
     $form = $event->getForm();
     if (null === ($data = $event->getData())) {
         $data = array('autocomplete' => array());
         $event->setData($data);
     }
     $options = $form->get('autocomplete')->getConfig()->getOptions();
     $options['choices'] = $options['em']->getRepository($options['class'])->findBy(array($options['id_reader']->getIdField() => $data['autocomplete']));
     if (isset($options['choice_list'])) {
         // clear choice list for SF < 3.0
         $options['choice_list'] = null;
     }
     $form->add('autocomplete', LegacyFormHelper::getType('entity'), $options);
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $entity = $options['entity'];
     $view = $options['view'];
     $entityConfig = $this->configManager->getEntityConfig($entity);
     $entityProperties = $entityConfig[$view]['fields'];
     $formGroups = array();
     $currentFormGroup = null;
     foreach ($entityProperties as $name => $metadata) {
         $formFieldOptions = $metadata['type_options'];
         // Configure options using the list of registered type configurators:
         foreach ($this->configurators as $configurator) {
             if ($configurator->supports($metadata['fieldType'], $formFieldOptions, $metadata)) {
                 $formFieldOptions = $configurator->configure($name, $formFieldOptions, $metadata, $builder);
             }
         }
         $formFieldType = LegacyFormHelper::getType($metadata['fieldType']);
         // if the form field is a special 'group' design element, don't add it
         // to the form. Instead, consider it the current form group (this is
         // applied to the form fields defined after it) and store its details
         // in a property to get them in form template
         if (in_array($formFieldType, array('easyadmin_group', 'JavierEguiluz\\Bundle\\EasyAdminBundle\\Form\\Type\\EasyAdminGroupType'))) {
             $currentFormGroup = $metadata['fieldName'];
             $formGroups[$currentFormGroup] = $metadata;
             continue;
         }
         // 'divider' and 'section' are 'fake' form fields used to create the design
         // elements of the complex form layouts: define them as unmapped and non-required
         if (0 === strpos($metadata['property'], '_easyadmin_form_design_element_')) {
             $formFieldOptions['mapped'] = false;
             $formFieldOptions['required'] = false;
         }
         $formField = $builder->getFormFactory()->createNamedBuilder($name, $formFieldType, null, $formFieldOptions);
         $formField->setAttribute('easyadmin_form_group', $currentFormGroup);
         $builder->add($formField);
     }
     $builder->setAttribute('easyadmin_form_groups', $formGroups);
 }
 /**
  * Creates the form used to delete an entity. It must be a form because
  * the deletion of the entity are always performed with the 'DELETE' HTTP method,
  * which requires a form to work in the current browsers.
  *
  * @param string $entityName
  * @param int    $entityId
  *
  * @return Form
  */
 protected function createDeleteForm($entityName, $entityId)
 {
     /** @var FormBuilder $formBuilder */
     $formBuilder = $this->get('form.factory')->createNamedBuilder('delete_form')->setAction($this->generateUrl('easyadmin', array('action' => 'delete', 'entity' => $entityName, 'id' => $entityId)))->setMethod('DELETE');
     $submitButtonType = LegacyFormHelper::useLegacyFormComponent() ? 'submit' : 'Symfony\\Component\\Form\\Extension\\Core\\Type\\SubmitType';
     $formBuilder->add('submit', $submitButtonType, array('label' => 'delete_modal.action', 'translation_domain' => 'EasyAdminBundle'));
     return $formBuilder->getForm();
 }