2017-08-29 55 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的信息