Luego el controller
Código PHP:
Ver originalclass WelcomeController extends Controller
{
/**
* @Route("/", name="home")
* @Template()
*/
public function indexAction()
{
$post = new Post();
$request = $this->getRequest();
$form = $this->createForm(new PostType(), $post);
if('POST' == $request->getMethod()){
$form->bindRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($post);
$this->get('session')->setFlash('notice', 'Post created successfully!');
return $this->redirect($this->generateUrl('home'));
}
}
return array('form' => $form->createView()); }
}
La presentación del form
Código twig:
Ver original{% extends 'AcmeDemoBundle::layout.html.twig' %}
{% block content %}
<form action="" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<input type="submit" />
</form>
<ul class="record_actions">
<li>
<a href="#" class="jslink">
Add a Image
</a>
</li>
</ul>
{% endblock %}
y por último unos bits de js
Código Javascript
:
Ver originalgoogle.load("jquery", "1.7.1");
google.setOnLoadCallback(function(){
(function($){
function add() {
var collectionHolder = $('#acme_demobundle_posttype_images');
var prototype = collectionHolder.attr('data-prototype');
form = prototype.replace(/\$\$name\$\$/g, collectionHolder.children().length);
form = $(form).filter('div').first().html($(form).html() +'<a class="delete_link" href="#">remove</a>');
collectionHolder.append(form);
}
$('a.jslink').click(function(event){
event.preventDefault();
add();
});
$(document).on('click', 'a.delete_link',function(event){
$(event.currentTarget).parent().remove();
});
})(jQuery);
});
Como te podrás dar cuenta el drawback de todo esto es la dependencia en js, igual creo que la idea se entiende y espero que te sirva al menos de base.
Saludos.