How to create a form with a bunch of unrelated objects in Symfony2?

I am working on a log parser that uses a database table to "translate" log events into human equivalents for reporting.

For example, a log entry such as "start_application_minecraft" is converted to " Start Minecraft . "

I am trying to create a web interface for adding / updating displayed text, but I cannot figure out how to get them into a Symfony Form object .

I have a LogEvent object (with properties for ID , Text and DisplayText ), and I created a form type that matches these properties.

It works great for changing one event at a time, but I would like all of them to be on the same page with one submit button to update everything. The problem is that all the documentation that I can find when embedding forms is related to objects that are related (for example, a category containing several Products), but in my case all the entities that I need to work with are not completely connected. What is the best way to configure this?

+5
source share
2 answers

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) { // perform any logic you need to here // (ex: removing the log event; $em->remove($logEvent);) } $em->flush(); } return $this->redirect($this->generateUrl('log_event_edit')); } 
+1
source

You can create a type of form that adds all entities as children. IF you make your "data" an array, you can have an arbitrary number of form elements.

+1
source

Source: https://habr.com/ru/post/1209281/


All Articles