symfony - Embed a Collection of Forms in symfony2 -


hey guys i'm new @ symfony2 , i'm trying simple application!

i have 3 entities, called "carinquerito", "carpergunta" , "carresposta" , related can see on "interesting" code below. these properties have appropriate gets/sets.

what pretend when create form carinquerito, embed form carpergunta , form carresposta carpergunta form. i've accomplished embedding collection of carresposta carpergunta form, when try embed result of carinquerito seems not work.

class carinquerito {      /**      * @orm\onetomany(targetentity="carpergunta", mappedby="idinquerito", cascade={"persist"})      */     private $perguntas; }  class carinqueritotype{     $builder->add('perguntas', 'collection', array(             'type' => new carperguntatype(),             'allow_add' => true,             'by_reference' => false,             'prototype' => true,             'allow_delete' => true,             )); }   class carinquerito {  /**      * @var carinquerito      *      * @orm\manytoone(targetentity="carinquerito", inversedby="perguntas")      * @orm\joincolumns({      *   @orm\joincolumn(name="id_inquerito", referencedcolumnname="id", onupdate="cascade", ondelete="cascade", nullable = false)      * })      */     private $idinquerito;    /**      * @orm\onetomany(targetentity="carresposta", mappedby="idpergunta", cascade={"persist"})      */     private $respostas; }  class carperguntatype{    $builder->add('respostas', 'collection', array(             'type' => new carrespostatype(),             'allow_add' => true,             'by_reference' => false,             'prototype' => true,             'allow_delete' => true,             )); }  class carresposta{    /**      * @var carpergunta      *      * @orm\manytoone(targetentity="carpergunta", inversedby="respostas")      * @orm\joincolumns({      *   @orm\joincolumn(name="id_pergunta", referencedcolumnname="id", onupdate="cascade", ondelete="cascade", nullable = false)      * })      */     private $idpergunta; }   class carrespostatype{   $builder->add('resposta', 'text'); } 

now controller:

public function newaction() {          $carinquerito = new carinquerito();         $form = $this->createform(new carinqueritotype(), $carinquerito);          return $this->render('careadminbundle:carinquerito:new.html.twig', array(                     'form' => $form->createview(),                 ));     } 

now views: new.html.twig:

<div class="inqueritos"> <div class="perguntas-list"> <ul class="perguntas" data-prototype="{% filter escape %}{% include 'careadminbundle:carinquerito:prototypepergunta.html.twig' {'form': form.perguntas.get('prototype')} %}{% endfilter %}">         {% pergunta in form.perguntas %}             <li>             {{ form_widget(pergunta.pergunta) }}              </li>         {% endfor %}         </ul> </div> </div> 

---protoypepergunta.html.twig----------

<div class="respostas-list" style="background-color: red; padding: 10px;">     <ul class="respostas" data-prototype="{{ form_widget(form.respostas.get('prototype')) | e }}">        {% resposta in form.respostas %}         <li>{{ form_widget(resposta.resposta) }}</li>      {% endfor %}      </ul> </div> 

i have javascript file add , remove links add "pergunta" , "respostas" below:

$(document).ready(function(){          var collectionholderperguntas = $('ul.perguntas');      collectionholderperguntas.find('li').each(function() {         addperguntaformdeletelink($(this));     });      // setup "add tag" link     var $addperguntalink = $('<a href="#" class="add_pergunta_link">adicionar pergunta</a>');     var $newlinkli = $('<li></li>').append($addperguntalink);        // add "add tag" anchor , li tags ul     collectionholderperguntas.append($newlinkli);      $addperguntalink.on('click', function(e) {         // prevent link creating "#" on url         e.preventdefault();          // add new tag form (see next code block)         addperguntaform(collectionholderperguntas, $newlinkli);     });      var collectionholderrespostas = $('ul.respostas');     collectionholderrespostas.find('li').each(function() {         alert("ol");         addrespostaformdeletelink($(this));     });      // setup "add tag" link     var $addrespostalink = $('<a href="#" class="add_resposta_link">adicionar resposta</a>');     var $newlinkliresposta = $('<li></li>').append($addrespostalink);      // add "add tag" anchor , li tags ul     collectionholderrespostas.append($newlinkliresposta);      $addrespostalink.on('click', function(e) {         // prevent link creating "#" on url         e.preventdefault();          // add new tag form (see next code block)         addrespostaform(collectionholderrespostas, $newlinkliresposta);     });   });  function addperguntaform(collectionholderperguntas, $newlinkli) {     // data-prototype explained earlier     var prototype = collectionholderperguntas.attr('data-prototype');      // replace '$$name$$' in prototype's html     // instead number based on current collection's length.     var newform = prototype.replace(/\$\$name\$\$/g, collectionholderperguntas.children().length);      // display form in page in li, before "add tag" link li     var $newformli = $('<li></li>').append(newform);     $newlinkli.before($newformli);       // add delete link new form     addperguntaformdeletelink($newformli); }  function addperguntaformdeletelink($perguntaformli) {     var $removeforma = $('<a href="#">apagar</a>');     $perguntaformli.append($removeforma);      $removeforma.on('click', function(e) {         // prevent link creating "#" on url         e.preventdefault();          // remove li tag form         $perguntaformli.remove();     }); }  function addrespostaform(collectionholderrespostas, $newlinkliresposta) {      // data-prototype explained earlier     var prototype = collectionholderrespostas.attr('data-prototype');     //      // replace '$$name$$' in prototype's html     // instead number based on current collection's length.     var newformresposta = prototype.replace(/\$\$name\$\$/g, collectionholderrespostas.children().length);      // display form in page in li, before "add tag" link li     var $newformliresposta = $('<li></li>').append(newformresposta);     $newlinkliresposta.before($newformliresposta);      // add delete link new form     addrespostaformdeletelink($newformliresposta); }  function addrespostaformdeletelink($respostaformli) {     var $removeforma = $('<a href="#">apagar</a>');     $respostaformli.append($removeforma);      $removeforma.on('click', function(e) {         // prevent link creating "#" on url         e.preventdefault();          // remove li tag form         $respostaformli.remove();     }); } 

any suggestions guys? search day, , have tried lot of things didn't manage find solution.

any appreciated!

thanks

you need put data in object 'carinquerito' in newaction(). example in form collection cookbook:

$tag1 = new tag();         $tag1->name = 'tag1';         $task->gettags()->add($tag1);         $tag2 = new tag();         $tag2->name = 'tag2';         $task->gettags()->add($tag2);         // end dummy code          $form = $this->createform(new tasktype(), $task); 

if don't embeded forms not create.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -