Q:

data-prototype not correctly rendered using nested collection form fields in a symfony2 form-type

Im building a project with symfony2.2, I'm posting here because i'm having trouble with nested collection, within collection.
I want to render a form, for this kind of Doctrine Entities :
http://i.stack.imgur.com/QmESP.jpg
But here, the period object is not rendering, while rendering it alone works perfectly.
The data-prototype is, in fact, correct but it's not rendering the data-prototype nested inside the main data-prototype.
So the question is : how do I correctly print the nested data-prototype within the main data-prototype ?
Twig template :
<fieldset class="nestedFormWeekDay" data-prototype="{{ form_widget(form.weekDays.day.vars.prototype)|e }}">
<legend>Weekday</legend>
</fieldset>
    {{ func.add_delete_form_elem('fieldset.nestedFormWeekDay', '<fieldset><legend>day</legend>', '</fieldset>') }}
</fieldset>

HTML for the main prototype: 
<html>
<head></head>
<body>
    <div id="timebundle_jsschedulertype_weekDays_day___name__">
        <div class="formSlot">
            <input id="timebundle_jsschedulertype_weekDays_day___name___isUltimo" type="hidden" name="timebundle_jsschedulertype[weekDays][day][__name__][isUltimo]">
            <div class="formSlot">
                <p class="formSlot">
                <p class="form"></p>
                <div id="timebundle_jsschedulertype_weekDays_day___name___period" data-prototype="<div class="formSlot"> <p class="formSlot"><label class="required">__name__label__</label></p> <p class="form"><div id="timebundle_jsschedulertype_weekDays_day___name___period___name__"><div class="formSlot"><p class="formSlot"><label class="required">Starttime</label></p><p class="form"><div id="timebundle_jsschedulertype_weekDays_day___name___period___name___startTime"><div class="formSlot"><p class="formSlot"><label class="required">Singlestarttime</label></p><p class="f...><option value="45">45</option><option value="46">46</option><option value="47">47</option><option value="48">48</option><option value="49">49</option><option value="50">50</option><option value="51">51</option><option value="52">52</option><option value="53">53</option><option value="54">54</option><option value="55">55</option><option value="56">56</option><option value="57">57</option><option value="58">58</option><option value="59">59</option></select></div></p></div></div></p></div></div></p> </div> ">
        </div>
            <p></p>
    </div>
</body>
</html>

SchedulerType :
->add('weekDays', new JSWeekdaysType(), array('required' => false))

WeekDaysType :
->add('day', 'collection', array('type' => new JSDayType(), 'allow_add' => true, 'by_reference' => false, 'allow_delete' => true))

DaysType:
->add('period', 'collection', array('type' => new JSPeriodType(), 'allow_add' => true, 'by_reference' => false, 'allow_delete' => true))

PeriodType:
->add('startTime', new JSSingleStartType())
->add('timeSlot',  new JSTimeSlotType());

SchedulerController:
$Scheduler = new JSScheduler();
$SchedulerType = new JSSchedulerType();
$form = $this->createForm($SchedulerType, $Scheduler);
return $this->render('TimeBundle:Form:newJsScheduler.html.twig', array('form' => $form->createView()));

And finally the javascript function that allow me to add new entities : 
function add_element($container, $wrap_beg, $wrap_end)
{
    var index = $container.find(':input').length;
    var input = $container.attr('data-prototype').replace(/__name__/g, index);
    $input = '<div class =\'new\'>'+$wrap_beg+input+$wrap_end+'<hr/></div>';
    $container.append($input);
}

full html : http://pastebin.com/sL8Z3Nxc

A:

Well, the only way for rendering is to turn the jquery function 'add_delete' into recursive.
And do something like :
function handle_nested_form(container)
{
  if (typeof container != 'object' || typeof container.children != 'function' )
     return;
  if (typeof container.attr('data-prototype') != 'undefined')
  {
    //Do whatever you need to do with the 'data-prototype' element 
  }
  var size = container.children().length;
  if (size)
  {
     container.children().each(function(){ handle_nested_form($(this))});
  }

}

