2015-10-26 56 views
0

我有一個問題,我一直無法解決。我有2個實體:Symfony2集合總是空的

<?php 
namespace ...\Entity; 

// ... 

/** 
* Pregunta 
* 
* @ORM\Table(name="pregunta", indexes={@ORM\Index(name="fk_respuesta_tipo_respuesta1_idx", columns={"tipo_respuesta_id"}))}) 
* @ORM\Entity 
*/ 
class Pregunta { 

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

    /** 
    * @var \Doctrine\Common\Collections\Collection 
    * 
    * @ORM\ManyToMany(targetEntity="...\Entity\Respuesta", mappedBy="pregunta") 
    */ 
    private $respuesta; 


    public function __construct() { 
     $this->tipoPrueba = new \Doctrine\Common\Collections\ArrayCollection(); 
     $this->respuesta = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    /** 
    * Add respuesta 
    * 
    * @param ...\Entity\Respuesta $respuesta 
    * @return Pregunta 
    */ 
    public function addRespuesta(...\Entity\Respuesta $respuesta) { 
     $this->respuesta[] = $respuesta; 

     return $this; 
    } 

    /** 
    * Remove respuesta 
    * 
    * @param ...\Entity\Respuesta $respuesta 
    */ 
    public function removeRespuesta(...\Entity\Respuesta $respuesta) { 
     $this->respuesta->removeElement($respuesta); 
    } 

    /** 
    * Get respuesta 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getRespuesta() { 
     return $this->respuesta; 
    } 

    function setRespuesta(\Doctrine\Common\Collections\Collection $respuesta) { 
     $this->respuesta = $respuesta; 
    } 
} 

然後我有Respuesta實體:

<?php 
class Respuesta { 

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

    /** 
    * @var string 
    * 
    * @ORM\Column(name="texto_respuesta", type="text", nullable=false) 
    */ 
    private $textoRespuesta; 

    // ... 

我有一個PreguntaType有其田和RespuestaType集合:

/** 
* @param FormBuilderInterface $builder 
* @param array $options 
*/ 
public function buildForm(FormBuilderInterface $builder, array $options) { 
    $builder 
      ->add('titulo', 'textarea', array("label" => "Enunciado: ", "required" => true, "attr" => array('class' => 'form-control'))) 
      ->add('numeroPagina', 'integer', array("label" => "Página: ", "required" => true, "attr" => array('class' => 'form-control'))) 
      ->add('areaConocimiento', 'entity', array('class' => 'UciBaseDatosBundle:AreaConocimiento', 'required' => false, 'attr' => array('style' => 'width: 100%'))) 
      ->add('trianguloTalento', 'entity', array('class' => 'UciBaseDatosBundle:TrianguloTalento', 'required' => false, 'attr' => array('style' => 'width: 100%'))) 
      ->add('capitulo', 'entity', array('class' => 'UciBaseDatosBundle:Capitulo', 'required' => false, 'attr' => array('style' => 'width: 100%'))) 
      ->add('grupoProcesos', 'entity', array('class' => 'UciBaseDatosBundle:GrupoProcesos', 'required' => false, 'attr' => array('style' => 'width: 100%'))) 
      ->add('tipoPrueba', 'entity', array('class' => 'UciBaseDatosBundle:TipoPrueba', 'expanded' => true, 'multiple' => true, 'required' => false, 'attr' => array('style' => 'width: 100%'))) 
      ->add('libro', 'entity', array('class' => 'UciBaseDatosBundle:Libro', 'required' => false, 'attr' => array('style' => 'width: 100%'))) 
      ->add('respuesta', 'collection', array(
       'type' => new RespuestaType(), 
       'prototype' => true, 
       'allow_add' => true, 
       'by_reference' => false, 
       'allow_delete' => true, 
       'label' => ' ' 
    )); 
} 

/** 
* @param OptionsResolverInterface $resolver 
*/ 
public function setDefaultOptions(OptionsResolverInterface $resolver) { 
    $resolver->setDefaults(array(
     'data_class' => 'Uci\Bundle\BaseDatosBundle\Entity\Pregunta' 
    )); 
} 

然而,當我調試我的表單提交,如果我將我的集合設置爲'required' => true,則會拋出此錯誤An invalid form control with name='...[respuesta][Respuesta0][RespuestaField]' is not focusable。另一方面,如果我將它設置爲'required' => false,我的集合的字段總是空的。

這是我的嫩枝文件:

<form action="{{ path('uci_administrador_registrarPregunta', { 'idTipoRespuesta': tipoRespuesta.id }) }}" name="formulario" method="POST" enctype="multipart/form-data">    
    <h3 class="thin text-center">Registrar una nueva pregunta {{ tipoRespuesta.nombre }}</h3> 
    <p class="text-center text-muted">{{ tipoRespuesta.explicacion }}</p> 
    <hr> 

    {% if error %} 
     <div style="color:red">{{ error }}</div> 
    {% endif %} 

    <div class="row top-margin"> 
     <div class="cols-xs-12 col-sm-10 col-md-8 col-lg-8"> 
      {{ form_row(form.titulo) }} 
     </div> 
    </div> 
    <div class="row top-margin"> 
     <div class="cols-xs-12 col-sm-10 col-md-8 col-lg-8"> 
      {{ form_row(form.numeroPagina) }} 
     </div> 
    </div> 
    <div class="row top-margin"> 
     <div class="cols-xs-12 col-sm-10 col-md-8 col-lg-8"> 
      {{ form_row(form.areaConocimiento) }} 
     </div> 
    </div> 
    <div class="row top-margin"> 
     <div class="cols-xs-12 col-sm-10 col-md-8 col-lg-8"> 
      {{ form_row(form.capitulo) }} 
     </div> 
    </div> 
    <div class="row top-margin"> 
     <div class="cols-xs-12 col-sm-10 col-md-8 col-lg-8"> 
      {{ form_row(form.grupoProcesos) }} 
     </div> 
    </div> 
    <div class="row top-margin"> 
     <div class="cols-xs-12 col-sm-10 col-md-8 col-lg-8"> 
      {{ form_row(form.trianguloTalento) }} 
     </div> 
    </div> 
    <div class="row top-margin"> 
     <div class="cols-xs-12 col-sm-10 col-md-8 col-lg-8"> 
      {{ form_row(form.tipoPrueba) }} 
     </div> 
    </div> 
    <div class="row top-margin"> 
     <div class="cols-xs-12 col-sm-10 col-md-8 col-lg-8"> 
      {{ form_row(form.libro) }} 
     </div> 
    </div> 
    <br> 
    <hr> 
    <h3>Respuestas</h3><br> 
    <div class="respuestas" data-prototype="{{ form_widget(form.respuesta.vars.prototype)|e }}"> 
     {# iterate over each existing tag and render its only field: name #} 
     {% for respuesta in form.respuesta %} 
      <div class="row top-margin"> 
       <div class="cols-xs-12 col-sm-10 col-md-8 col-lg-8"> 
        {{ form_row(respuesta.correcta) }} 
       </div> 
      </div> 
     {% endfor %} 
    </div> 
    <br><br> 
    <div class="row"> 
     <div class="col-lg-8">      
     </div> 
     <div class="col-lg-4 text-right"> 
      <button class="btn btn-action" type="submit">Registrar</button> 
     </div> 
    </div> 
    {{ form_rest(form) }} 
</form> 

我用JavaScript來加我收集表格:

var $collectionHolder; 
// setup an "add a tag" link 
var $addTagLink = $('<a href="#" class="add_tag_link">Añadir respuesta</a>'); 
var $newLinkLi = $('<div></div>').append($addTagLink); 
function addTagForm($collectionHolder, $newLinkLi) { 
    // Get the data-prototype explained earlier 
    var prototype = $collectionHolder.data('prototype'); 
    // get the new index 
    var index = $collectionHolder.data('index'); 
    // Replace '__name__' in the prototype's HTML to 
    // instead be a number based on how many items we have 
    var newForm = prototype.replace(/__name__/g, 'Respuesta' + index); 
    // increase the index with one for the next item 
    $collectionHolder.data('index', $collectionHolder.find(':input').length); 
    // Display the form in the page in an li, before the "Add a tag" link li 
    var $newFormLi = $('<div style="background-color:#F6F6F6; border-radius:10px;padding: 25px;border: 5px solid #003c70;margin: 5px;"></div><br>').append(newForm); 
    $newLinkLi.before($newFormLi); 
} 
document.ready = function() { 
    // Get the ul that holds the collection of tags 
    $collectionHolder = $('div.respuestas'); 
    // add the "add a tag" anchor and li to the tags ul 
    $collectionHolder.append($newLinkLi); 
    // count the current form inputs we have (e.g. 2), use that as the new 
    // index when inserting a new item (e.g. 2) 
    $collectionHolder.data('index', $collectionHolder.find(':input').length); 
    $addTagLink.on('click', function (e) { 
     // prevent the link from creating a "#" on the URL 
     e.preventDefault(); 
     // add a new tag form (see next code block) 
     addTagForm($collectionHolder, $newLinkLi); 
    }); 

    // ... 
} 

我真的很感激任何幫助。

謝謝。

+0

如何在前端顯示錶單?請在您的問題中添加模板中的一些代碼。 – paulgv

回答

0

您是否試圖隱藏表單中的某些字段?它看起來像一些字段是必需的,但實際上並未顯示在頁面中,這阻止了瀏覽器驗證表單。參考答案:https://stackoverflow.com/a/28340579/4114297

我想知道爲什麼你只在循環中渲染respuesta.correcta。你可能想渲染而respuesta項目:

<div class="respuestas" data-prototype="{{ form_widget(form.respuesta.vars.prototype)|e }}"> 
    {# iterate over each existing tag and render its only field: name #} 
    {% for respuesta in form.respuesta %} 
     <div class="row top-margin"> 
      <div class="cols-xs-12 col-sm-10 col-md-8 col-lg-8"> 
       {{ form_row(respuesta) }} {# <- Here #} 
      </div> 
     </div> 
    {% endfor %} 
</div> 

如果你需要一些領域被隱藏和/或預填充,你能做到這RespuestaType

+0

感謝您的幫助@paulvg。我沒有隱藏的領域。當我嘗試提交表單時,它會說:「名稱='uci_bundle_basedatosbundle_pregunta [respuesta] [Respuesta0] [textoRetroalimentacion]'的無效表單控件不可聚焦」,並且與其他Respuesta字段相同。我改變了循環,但它引發了同樣的錯誤。 –

1

我的問題是在我的JavaScript。我不知道爲什麼我以前的代碼不起作用,但我改變了它的工作原理。我的javascript如下:

var collectionHolder = $('#respuestas'); 
var prototype = collectionHolder.attr('data-prototype'); 
var form = prototype.replace(/__name__/g, collectionHolder.children().length); //importante 
var removeFormA = $('<a href="#" onclick="addTagFormDeleteLink(event, this);">Borrar</a>'); 
var newLi = $('<li></li>'); 
newLi.append(form); 
newLi.append(removeFormA); 
collectionHolder.append(newLi); 

這是我TWIG文件的一部分:

<ul id="respuestas" data-prototype="{{ form_widget(form.respuesta.vars.prototype)|e }}"> 
{% for respuesta in form.respuesta %} 
     <li> {{ form_row(respuesta) }}</li> 
{% endfor %} 
</ul> 

我希望它可以幫助別人。

問候。