How do I retrieve a form collection's prototype attribute from within a controller (Symfony2)?

StackOverflow https://stackoverflow.com/questions/17975877

  •  04-06-2022
  •  | 
  •  

Question

I'm having trouble getting a form collection's prototype attribute within a controller for inclusion in a JSON response, the furthest I have got (i.e. no errors are being thrown) is with the following, however the returned value is empty.

$form      = $this->createForm(new MyType());
$prototype = $form->get('myCollection')->getConfig()->getAttribute('prototype');

I've also tried creating the form's view, and pulling the attribute from there, however the prototype key is not defined here...

$form      = $this->createForm(new MyType());
$view      = $form->createView();
$prototype = $view->children['myCollection']->vars['attr']['prototype'];

Does anyone know where I'm going wrong?

(Symfony 2.2.4)

Was it helpful?

Solution

It seems I can get what I'm after by rendering just the prototype attribute of my form's collection field, this feels like the long way around, but it works.

// Controller method
$form      = $this->createForm(new MyType());
$view      = $form->createView()->children['myCollection'];
$prototype = $this->renderView('MyBundle:Foo:prototype.html.twig', array('form' => $view));
<!-- Template (MyBundle:Foo:prototype.html.twig) -->
{{ form_widget(form.vars.prototype) }}

OTHER TIPS

$prototype = $view->children['myCollection']->vars['prototype'];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top