Use the collection form field type for Symfony and use Doctrine to find the LogEvent objects you want and pass them to the collection.
Example: http://symfony.com/doc/current/cookbook/form/form_collections.html
Link: http://symfony.com/doc/current/reference/forms/types/collection.html
So, first you would create a LogEvent form type:
class LogEventType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('text'); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'My\Bundle\Entity\LogEvent', )); } public function getName() { return 'log_event'; } }
Then create your form type containing Collection of LogEvent objects:
class MultiLogEventType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add( 'logEvents', 'collection', array('type' => new LogEventType()) ); } public function getName() { return 'multi_log_event'; } }
Then create a form in your controller and pass the log events to it:
public function indexAction() { // replace findAll() with a more restrictive query if you need to $logEvents = $this->getDoctrine()->getManager() ->getRepository('MyBundle:LogEvent')->findAll(); $form = $this->createForm( new MultiLogEventType(), array('logEvents' => $logEvents) ); return array('form' => $form->createView()); }
Then, in your editing action, you can scroll through the log events and do whatever you need:
public function editAction(Request $request) { $em = $this->getDoctrine()->getManager(); $editForm = $this->createForm(new MultiLogEventType()); $editForm->handleRequest($request); if ($editForm->isValid()) { foreach ($logEvents as $logEvent) {
source share