2017-08-29 44 views
0

我在symfony上有一個表單集合的問題。 我有3個實體文章,AdditionnalFile,AdditionnalInformationSymfony表單集合定製原型

文章實體

/** 
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Category", inversedBy="articles") 
* @ORM\JoinColumn(nullable=false) 
* @Gedmo\Versioned 
*/ 
private $category; 

/** 
* @ORM\OneToMany(targetEntity="AppBundle\Entity\AdditionnalInformation", mappedBy="article", cascade={"persist", "remove"}) 
* @ORM\JoinColumn(nullable=true) 
*/ 
private $additionnalInformations; 

/** 
* @ORM\OneToMany(targetEntity="AppBundle\Entity\AdditionnalFile", mappedBy="article", cascade={"persist", "remove"}) 
* @ORM\JoinColumn(nullable=true) 
*/ 
private $additionnalFiles; 

AdditionnalInformation實體

/** 
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Article", inversedBy="additionnalInformations") 
*/ 
private $article; 

/** 
* @ORM\ManyToMany(targetEntity="UserLdapBundle\Entity\Group", inversedBy="additionnalInformations") 
* @ORM\JoinColumn(nullable=false) 
* 
* @Assert\Count(
*  min = 1, 
*  max = 5, 
*  minMessage = "Il faut au minimum 1 groupe autorisé", 
*  maxMessage = "Il faut au maximum {{ limit }} groupe autorisé" 
*) 
*/ 
private $groups; 

/** 
* @var int 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 

/** 
* @var string 
* 
* @ORM\Column(name="title", type="string", length=255) 
* @Gedmo\Versioned 
* @Assert\Type(type="string") 
* @Assert\NotBlank() 
*/ 
private $title; 


/** 
* @var string 
* 
* @ORM\Column(name="text", type="text") 
* @Gedmo\Versioned 
* @Assert\Type(type="string") 
* @Assert\NotBlank() 
*/ 
private $text; 

我不放棄最後的實體,因爲這並不重要 我創建一個表單類型爲AdditionnalFile

/** 
* @param FormBuilderInterface $builder 
* @param array    $options 
*/ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add(
      'title', 
      TextType::class, 
      array(
       'attr' => array(
        'placeholder' => 'Titre' 
       ), 
       'label' => 'Titre :' 
      ) 
     ) 
     ->add(
      'text', 
      TextareaType::class, 
      array(
       'attr' => array(
        'placeholder' => 'Texte' 
       ), 
       'label'  => 'Texte :' 
      ) 
     ) 
     ->add(
      'groups', 
      EntityType::class, 
      array(
       'attr'   => array(
        'placeholder' => 'Droits' 
       ), 
       'class'  => 'UserLdapBundle:Group', 
       'choice_label' => 'name', 
       'expanded'  => true, 
       'multiple'  => true, 
       'label'  => 'Accessible pour :' 
      ) 
     ); 
} 

/** 
* @param OptionsResolver $resolver 
*/ 
public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => AdditionnalInformation::class, 
    )); 
} 

而且我已創建我的文章formtype誰「嵌入」我additionnalInformationType

/** 
* @param FormBuilderInterface $builder 
* @param array    $options 
*/ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add(
      'title', 
      TextType::class, 
      array(
       'attr' => array(
        'placeholder' => 'Titre' 
       ), 
       'label' => 'Titre :' 
      ) 
     ) 
     ->add(
      'category', 
      EntityType::class, 
      array(
       'attr'   => array(
        'placeholder' => 'Catégorie' 
       ), 
       'class'  => 'AppBundle\Entity\Category', 
       'choice_value' => 'id', 
       'choice_label' => 'name', 
       'multiple'  => false, 
       'label'  => 'Catégorie :' 
      ) 
     ) 
     ->add(
      'text', 
      TextareaType::class, 
      array(
       'attr'  => array(
        'placeholder' => 'Texte', 
        'class'  => 'tinymce' 
       ), 
       'label' => 'Texte :', 
       'required' => false 
      ) 
     ) 
     ->add(
      'tags', 
      TextType::class, 
      array(
       'attr' => array(
        'placeholder' => 'Tags' 
       ), 
       'label' => 'Tags :' 
      ) 
     ) 
     ->add(
      'ticketNumber', 
      TextType::class, 
      array(
       'attr'  => array(
        'placeholder' => 'Numéro de ticket, 301, 302,' 
       ), 
       'label' => 'Numéro(s) de ticket :', 
       'required' => false 
      ) 
     ) 
     ->add(
      'groups', 
      EntityType::class, 
      array(
       'attr'   => array(
        'placeholder' => 'Droits' 
       ), 
       'class'  => 'UserLdapBundle:Group', 
       'choice_label' => 'name', 
       'expanded'  => true, 
       'multiple'  => true, 
       'label'  => 'Accessible pour :' 
      ) 
     ) 
     ->add(
      'additionnalInformations', 
      CollectionType::class, 
      array(
       'entry_type' => AdditionnalInformationType::class, 
       'allow_add' => true, 
       'label' => 'Information(s) additionnel(s) :', 
       'prototype' => true 
      ) 
     ) 
     ->add(
      'additionnalFiles', 
      CollectionType::class, 
      array(
       'entry_type' => AdditionnalFileType::class, 
       'allow_add' => true, 
       'label' => 'Fichier(s) :', 
       'prototype' => true 
      ) 
     ) 
     ->add(
      'save', 
      SubmitType::class, 
      array(
       'label' => 'Sauvegarder', 
       'attr' => array(
        'class' => 'btn-primary' 
       ) 
      ) 
     ); 

但現在我有一些問題... :) 我怎麼可以自定義的原型?我想使用一個引導面板並在裏面放置additionnalInformation表單。 並複製此添加其他AdditionnalInformation 這可能嗎?

回答

0

其實我已經使用這個,它

{% block _article_additionnalInformations_entry_row %} 
<br> 
<div class="panel panel-primary"> 
    <div class="panel-heading">Information supplémentaire <a href="#" class="btn btn-xs btn-danger pull-right"><span class="glyphicon glyphicon-remove confirmation-suppression"></span></a></div> 
    <div class="panel-body"> 
     <div class="row"> 
      <div class="col-lg-8"> 
       {{ form_row(form.title) }} 
       {{ form_row(form.text) }} 
      </div> 
      <div class="col-lg-4"> 
       {{ form_row(form.groups) }} 
      </div> 
     </div> 
    </div> 
</div> 

{%endblock%}

0

你應該嘗試寫一個自定義的樹枝主題。你可以在this page找到更多的信息。

例如,你可以嘗試把這個代碼在您的模板(你呈現你的表格):

{% form_theme form _self %} 

{% block _additionnalFiles_entry_widget %} 
    <tr> 
     <td>{{ form_widget(form.task) }}</td> 
     <td>{{ form_widget(form.dueDate) }}</td> 
    </tr> 
{% endblock %} 

只要確保使用正確的塊名。您可以通過了解how form fragment are named或僅檢查模板中的{{ dump(form) }}來完成此操作。

0

您好感謝您的回答,我有嘗試把這個代碼

{% block _additionnalInformations_entry_row %} 
    <br> 
    <div class="panel panel-primary"> 
     <div class="panel-heading">Information supplémentaire <a href="#" class="btn btn-danger"><span class="glyphicon glyphicon-remove confirmation-suppression"></span></a></div> 
     <div class="panel-body"> 
      <div class="row"> 
       <div class="col-lg-8"> 
        {{ form_row(form.title) }} 
        {{ form_row(form.text) }} 
       </div> 
       <div class="col-lg-4"> 
        {{ form_row(form.groups) }} 
       </div> 
      </div> 
     </div> 
    </div> 

{% endblock %} 

,並使用

{% form_theme form _self %} 

但沒有什麼變化:( 我已經把用於自定義外排塊我使用SF 3.2的信息