2016-02-05 78 views
1

在Symfony admin中,我有一個表單,其中第二個字段類型取決於所選的字段值。第二個字段可以是Symfony Url字段類型或奏鳴曲提供sonata_type_model_list字段類型。在Controller中使用Sonata字段類型創建表單

我已經創建了一個ajax請求到My Bundle Controller來返回包含所需字段的表單。

> /src/MyBundle/Controller/MyController.php 

namespace MyBundle\Controller 

use Sonata\AdminBundle\Controller\CRUDController; 
use Symfony\Component\HttpFoundation\Request; 
use Doctrine\ORM\Mapping\ClassMetadataInfo; 
use Sonata\AdminBundle\Form\FormMapper; 

class MyController extends CRUDController 
{ 
    public function getFieldAction() 
    { 
    //getting the value of choice field 
    $type = $this->get('request')->get('type'); 

    //sonata.admin.reference is a service name of ReferenceBundle admin class 
     $fieldDescription = $this->admin->getModelManager() 
     ->getNewFieldDescriptionInstance($this->admin->getClass(), 'reference'); 
     $fieldDescription->setAssociationAdmin($this->container->get('sonata.admin.reference')); 
     $fieldDescription->setAdmin($this->admin); 
     $fieldDescription->setAssociationMapping(array(
      'fieldName' => 'reference', 
      'type' => ClassMetadataInfo::ONE_TO_MANY, 
     )); 

    // Getting form mapper in controller: 
    $contractor = $this->container->get('sonata.admin.builder.orm_form'); 
    $mapper = new FormMapper($contractor, $this->admin->getFormBuilder(), $this->admin); 

    $form_mapper = $mapper->add('reference', 'sonata_type_model_list', array(
      'translation_domain' => 'ReferenceBundle', 
      'sonata_field_description' => $fieldDescription, 
      'class' => $this->container->get('sonata.admin.reference')->getClass(), 
      'model_manager' => $this->container->get('sonata.admin.reference')->getModelManager(), 
      'label' => 'Reference', 
      'required' => false, 
     )); 


    //@ToDo build $form from $form_mapper 


    return $this->render('MyBundle:Form:field.view.html.twig', array(
     'form' => $form->createView(), 
    )); 
    } 
} 

我無法找到索納塔\ AdminBundle \表格\ FormMapper類中的任何方法來建立一個表(它似乎是可能的創建()方法,但它僅與普通的Symfony字段類型的作品,不奏鳴曲表單字段類型,通常在Block或Admin類中生成)。

是否有可能使用索納塔\ AdminBundle \表格\ FormMapper在控制器建立一個形式? 或者還有另一種方式,我可以用控制器中的奏鳴曲表單字段類型構建表單嗎?

+2

爲什麼不使用您的管理員類別製作表格?這是爲了這個 – chalasr

回答

0

您不應使用控制器,而應使用Sonata管理服務。

Sonata爲您提供'sonata_type_choice_field_mask'類型,它允許您根據此'sonata_type_choice_field_mask'輸入的值動態更改窗體上顯示的字段。

Here is the doc在這裏你可以找到關於奏鳴曲類型和選擇領域掩碼的一切。

protected function configureFormFields(FormMapper $formMapper) 
{ 
    $formMapper 
     ->add('reference', 'sonata_type_choice_field_mask', array(
      'choices' => array(
       //The list of available 'Reference' here 
       'choice1', 
       'choice2' 
      ), 
      'map' => array(
       //What you want to display depending of the selected option 
       'choice1' => array(
        // List of the fields displayed if choice 1 is selected 
        'field1', 'field3' 
       ), 
       'choice2' => array(
        // List of the fields displayed if choice 2 is selected 
        'field2', 'field3' 
       ) 
      ), 
      'placeholder' => 'Choose an option', 
      'required' => false 
     )) 
     ->add('field1', 'sonata_type_model_list', array(/* Options goes here */)) 
     ->add('field2', 'url', array(/* Options goes here */)) 
     ->add('field3') 
    ; 
} 
相關問題