Jovan Perovic
Jovan Perovic

Reputation: 20193

Symfony2 not rendering collection form data-prototype

OK, this is rather awkward problem. I'm using collection form type in Symfony and after I configure it with

'prototype' => true,
'allow_add' => true

I would expect it to print prototype when I call either one of these in Twig template:

form_rest(form)
form_widget(form.subform.get('prototype'))

The problem is that Symfony prints that prototype only if collection contains at least one element.

Is this a bug I'm not aware of or I'm missing something?

Any idea would be greatly appreciated! Thanks!

Upvotes: 0

Views: 2596

Answers (3)

Alexey Kosov
Alexey Kosov

Reputation: 3128

If the form data are empty, you can create a clone of the form and populate it with a dummy object. Use this clone for generating the prototype.

Upvotes: 0

Jovan Perovic
Jovan Perovic

Reputation: 20193

I have updated Symfony to it's latest stable and prototype is still acting strange:

  • If no items exist it would render <div data-prototype="...."></div>
  • Otherwise it would render prototype as pure HTML (not as DIV's attribute above)

Since no out-of-box solution exists, I was forced to do IF-ELSE in my JS. That is, JS first checks for data-prototype element and falls back to other one if it does not exists.

As soon as I have some time to spare I will study this behavior and, if it's turn out to be undesired (as I suspect it is), will report it as a bug...

Upvotes: 2

greg0ire
greg0ire

Reputation: 23255

I don't know if this is a bug, but I think there is no point in printing a form for the collection if it does not contain at least one element. If you're creating the collection, then you should create a dummy element and add it to your collection. This is not done automatically.

Upvotes: 0

Related Questions