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

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

  •  04-06-2022
  •  | 
  •  

문제

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)

도움이 되었습니까?

해결책

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) }}

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top