javascript - New forms - embedding a collection of forms - Symfony2 -
i'm doing same explained here: http://symfony.com/doc/current/cookbook/form/form_collections.html
but in case want add new "tags" not manually clicking on link, automatically. give template array items , each of items want add new form - number of items should equal number of forms.
if it's possible, i'd prefer solution this:
{% in items %} {{ i.name }} {{ form_widget(form.tags[loop.index0].name) }} {% endfor %}
but how automatically create objects in controller, too? tells me there no obeject index=1, , yes - there isn't, isn't there way create them automatically , not need create example 10 empty objects of same kind in controller? :(
another thing thinking this:
{% in items %} <ul class="orders" data-prototype="{{ form_widget(form.orders.vars.prototype)|e }}"> {{ i.name }} , here should field form, example tag.name </ul> {% endfor %}
i suggest js given in cookbook should changed this, i'm not in js , tries didn't job.
i tried putting in loop:
<script> addtagform(collectionholder); </script>
and in .js file:
var collectionholder = $('ul.orders'); jquery(document).ready(function() { collectionholder.data('index', collectionholder.find(':input').length); function addtagform(collectionholder) { var prototype = collectionholder.data('prototype'); var index = collectionholder.data('index'); var newform = prototype.replace(/__name__/g, index); collectionholder.data('index', index + 1); var $newformli = $('<li></li>').append(newform); } });
assuming main class has addtag($tag) method, can add different 'new' tags it.
in class task
public function addtag($tag){ $this->tags[]=$tag; return $this; }
in controller (assuming 10 tags here)
$task=new task(); for($i=0;i<10;i++){ $task->addtag(new tag()); } $form->setdata($task);
in view
{% tag in form.tags %} <ul class="orders"> <li>{{ form_widget(tag.name) }}</li> </ul> {% endfor %}
if don't need manually click, can remove javascript part.
Comments
Post a Comment