我有两种形式:
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
));
}
如果我的收藏品不是空的,例如我正在编辑时,它会呈现正常状态。但是当我添加一个新条目时,form_end(form)会在html的末尾呈现这个:
<div>
<label class="required">Fotos</label>
<div id="galeria_fotos" data-prototype=""></div>
</div>
这是我收集的表单html:
{{ 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) }}
控制器:
<?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'));
}
}
答案 0 :(得分:1)
您必须创建一个对象并将其与控制器中的表单相关联。请参阅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(),
));
}
}
因此,您必须创建一个GaleriaFotoType
对象并将其添加到fotos
集合中:
public function adicionarAction(Request $request)
{
$galeria = new Galeria;
$galeria->getFotos()->add(new Foto());
$form = $this->createForm(new GaleriaType, $galeria);
$form->handleRequest($request);
[...]