Skip to content

Instantly share code, notes, and snippets.

@jmclean
Created December 17, 2013 21:39
Show Gist options
  • Save jmclean/8013037 to your computer and use it in GitHub Desktop.
Save jmclean/8013037 to your computer and use it in GitHub Desktop.
{% extends 'AcmeDemoBundle::layout.html.twig' %}
{% block title "Form test" %}
{% form_theme form.questions _self %}
{% block content %}
<form action="" method="POST">
{{ form_rest(form) }}
<input type="submit" value="Send" />
</form>
{% endblock %}
{% block _tp_assignment_questions_row %}
<div class="collection">
<a class="add_item" data-prototype="{{ form_row(form.vars.prototypes.open_question) | escape }}" href="#">+ open question</a>
<a class="add_item" data-prototype="{{ form_row(form.vars.prototypes.choice_question) | escape }}" href="#">+ choice question</a>
{{ form_widget(form) }} {# Note that this generates <div id="tp_assignment_questions"> which is used in the Javascript #}
</div>
<script type="text/javascript">
if (!window.$) alert('jQuery not loaded');
$(function() {
if (!window.infinite || !window.infinite.Collection) alert('Collections helper not loaded');
var questions = $('#tp_assignment_questions');
new window.infinite.Collection(questions, questions.siblings('.add_item'));
});
</script>
{% endblock %}
{% block open_question_row %}
<div class="item">
<h4>Open Question <a class="remove_item" href="#">Remove</a></h4>
{{ form_widget(form) }}
</div>
{% endblock %}
{% block choice_question_row %}
<div class="item">
<h4>Choice Question <a class="remove_item" href="#">Remove</a></h4>
{{ form_widget(form) }}
</div>
{% endblock %}
<?php
namespace Acme\DemoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class OpenQuestionType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('_type', 'hidden', array(
'data' => $this->getName(),
'mapped' => false
));
}
public function getName()
{
return 'open_question';
}
public function getParent()
{
return 'question';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\DemoBundle\Model\OpenQuestion',
'model_class' => 'Acme\DemoBundle\Model\OpenQuestion',
));
}
}
<?php
namespace Acme\DemoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class TpAssignmentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('title', 'text');
$builder->add('questions', 'infinite_form_polycollection', array(
'types' => array('choice_question', 'open_question'),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
));
}
public function getName()
{
return 'tp_assignment';
}
}
@3kynox
Copy link

3kynox commented Nov 30, 2015

Hello again and thanks for support.

I almost get it to work, Displaying results looks like to be blank with "form_widget(form._type)"

Rendering ->

With {% for subForm in form %} {{ dump(subForm) }} {% endfor %} - (data is displayed in dump)

With dump inside ..._entry_row -> {{ dump(form._type) }} - no moyensComm data inside

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment