Example #1
0
 private function getThingEditForm(Application $app, Request $request, $thingId)
 {
     $error = $request->query->get('error');
     $repo = $app->getThingRepository();
     $add = false;
     $thing = $repo->getById($thingId);
     if ($thing === null) {
         $defaults = null;
         $add = true;
     } else {
         $defaults = ['name' => $thing->getName(), 'email' => $thing->getEmail(), 'description' => $thing->getDescription()];
     }
     $form = $app['form.factory']->createBuilder('form', $defaults)->add('name', 'text')->add('email', 'email')->add('description', 'textarea', array('required' => false))->getForm();
     // handle form submission
     $form->handleRequest($request);
     if ($form->isValid()) {
         $data = $form->getData();
         if ($add) {
             $thing = new Thing();
         }
         $thing->setEmail(strtolower($data['email']))->setName($data['name'])->setDescription($data['description']);
         if ($add) {
             if (!$repo->add($thing)) {
                 return $app->redirect($app['url_generator']->generate('things_add', array('error' => 'Failed adding thing')));
             }
         } else {
             $repo->update($thing);
         }
         return $app->redirect($app['url_generator']->generate('things_index'));
     }
     return new Response($app['twig']->render('edit.html.twig', ['form' => $form->createView(), 'thing' => $thing, 'error' => $error]));
 }