Reputation: 2176
I have this two forms:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nome')
->add('fotos', 'collection', array(
'type' => new GaleriaFotoType,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
));
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('caminho', 'file', array(
'required' => false,
'data_class' => null
));
}
If my collection is not empty, for example when I'm editing, it renders normal. But when I'm adding an new entry the form_end(form) renders this at the end of the html:
<div>
<label class="required">Fotos</label>
<div id="galeria_fotos" data-prototype=""></div>
</div>
This is my form html for the collection:
{{ form_label(form.fotos) }}
<ul id="galeria_foto" data-prototype="{{ form_widget(form.fotos.vars.prototype)|e }}">
{% for foto in form.fotos %}
<li>{{ form_widget(foto) }}</li>
{% endfor %}
</ul>
{{ form_errors(form.fotos) }}
Controller:
<?php
namespace DasArtes\AdminBundle\Controller;
use DasArtes\AdminBundle\Entity\Galeria;
use DasArtes\AdminBundle\Form\GaleriaType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\Common\Collections\ArrayCollection;
class GaleriasFotosController extends Controller
{
public function indexAction()
{
$galerias = $this->getDoctrine()->getRepository('DasArtesAdminBundle:Galeria')->findAll();
return $this->render('@DasArtesAdmin/galerias-de-fotos/index.html.twig', array(
'galerias' => $galerias
));
}
public function adicionarAction(Request $request)
{
$galeria = new Galeria;
$form = $this->createForm(new GaleriaType, $galeria);
$form->handleRequest($request);
if ($form->isValid()) {
// autor
$usuario_logado = $this->get('security.context')->getToken()->getUser();
$galeria->setAutor($usuario_logado);
// upload fotos
$uploadableManager = $this->get('stof_doctrine_extensions.uploadable.manager');
foreach ($form->get('fotos')->getData() as $foto) {
$uploadableManager->markEntityToUpload($foto, $foto->getCaminho());
}
$manager = $this->getDoctrine()->getManager();
$manager->persist($galeria);
$manager->flush();
$this->get('session')->getFlashBag()->add('alert-success', 'Galeria adicionada com sucesso');
return $this->redirect($this->generateUrl('das_artes_admin_galerias_de_fotos'));
}
return $this->render('@DasArtesAdmin/galerias-de-fotos/form.html.twig', array(
'form' => $form->createView(),
'tipo' => 'adicionar'
));
}
public function editarAction(Galeria $galeria, Request $request)
{
// salva as fotos originais para comparar depois com as fotos enviadas pelo form
$fotos = new ArrayCollection;
foreach ($galeria->getFotos() as $foto) {
$fotos->add($foto);
}
$form = $this->createForm(new GaleriaType, $galeria);
$form->handleRequest($request);
if ($form->isValid()) {
// autor
$usuario_logado = $this->get('security.context')->getToken()->getUser();
$galeria->setAutor($usuario_logado);
// upload fotos
$uploadableManager = $this->get('stof_doctrine_extensions.uploadable.manager');
foreach ($form->get('fotos')->getData() as $foto) {
if (!is_string($foto->getCaminho())) {
$uploadableManager->markEntityToUpload($foto, $foto->getCaminho());
}
}
$manager = $this->getDoctrine()->getManager();
// compara as fotos enviadas pelo form com as originais e remove se necessário
foreach ($fotos as $foto) {
if (!$galeria->getFotos()->contains($foto)) {
$galeria->getFotos()->removeElement($foto);
$manager->remove($foto);
}
}
$manager->flush();
$this->get('session')->getFlashBag()->add('alert-success', 'Galeria editada com sucesso');
return $this->redirect($this->generateUrl('das_artes_admin_galerias_de_fotos'));
}
return $this->render('@DasArtesAdmin/galerias-de-fotos/form.html.twig', array(
'form' => $form->createView(),
'tipo' => 'editar'
));
}
public function removerAction(Galeria $galeria)
{
$manager = $this->getDoctrine()->getManager();
$manager->remove($galeria);
$manager->flush();
// flash message
$this->get('session')->getFlashBag()->add('alert-success', 'Artista removido com sucesso');
return $this->redirect($this->generateUrl('das_artes_admin_galerias_de_fotos'));
}
}
Upvotes: 1
Views: 967
Reputation: 10513
You have to create an object and associate it to your form in your controller. See this example from the documentation:
class TaskController extends Controller
{
public function newAction(Request $request)
{
$task = new Task();
// dummy code - this is here just so that the Task has some tags
// otherwise, this isn't an interesting example
$tag1 = new Tag();
$tag1->name = 'tag1';
$task->getTags()->add($tag1);
$tag2 = new Tag();
$tag2->name = 'tag2';
$task->getTags()->add($tag2);
// end dummy code
$form = $this->createForm(new TaskType(), $task);
$form->handleRequest($request);
if ($form->isValid()) {
// ... maybe do some form processing, like saving the Task and Tag objects
}
return $this->render('AcmeTaskBundle:Task:new.html.twig', array(
'form' => $form->createView(),
));
}
}
So you will have to create a GaleriaFotoType
object and add it to the fotos
collection:
public function adicionarAction(Request $request)
{
$galeria = new Galeria;
$galeria->getFotos()->add(new Foto());
$form = $this->createForm(new GaleriaType, $galeria);
$form->handleRequest($request);
[...]
Upvotes: 1