public function createAction(Request $request) { $album = new Album(); $form = $this->createForm(new AlbumType(), $album); **$form$form->handleRequest($request);**; if ($form->isValid()) { // Handle the uploaded images $files = $form->getData()->getPicture(); // If there are images uploaded if($files != null) { $constraints = array('maxSize'=>'10M', 'mimeTypes' => array('image/*')); $uploadFiles = $this->get('app.fileuploader')->create($files, $constraints); if($uploadFiles->upload()) { $album->setPicture($uploadFiles->getFilePaths()); $em = $this->getDoctrine()->getEntityManager(); $em->persist($album); $em->flush(); $this->get('session')->getFlashBag()->add('notice', 'Las imagenes se han subido con éxito.'); } // If there are file constraint validation issues else { // Check for errors foreach($uploadFiles->getErrors() as $error) { $this->get('session')->getFlashBag()->add('error', $error); } return $this->render('FrontEndBundle:AlbumOld:uploadAlbum.html.twig', array( 'entity' => $album, 'form' => $form->createView(), )); } } } return $this->render('FrontEndBundle:AlbumOld:uploadAlbum.html.twig', array( 'form' => $form->createView() ));
Twig
{{ form_label(form.name) }} {{ form_widget(form.name) }}
{{ form_label(form.picture) }} {{ form_widget(form.picture, { 'full_name': 'simplex_album[picture]' ~ '[]' } ) }}
{% if error is defined %} {% for errorMessage in error %}
Album.php
add('name')
->add('ìcture', 'file', array(
'attr' => array(
'accept' => 'image/*',
'multiple' => 'multiple'
),
'data_class' => null
)
);
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'simplex\Entity\Album'
));
}
/**
* @return string
*/
public function getName()
{
return 'simplex_album';
}
}