php - Symfony 2: Multiple forms generated list of objects -


i generate page list of users names , next every user have button activate/deactivate user.

i of course create link user id , method in controller perform operation when link clicked. far know not recommended though way due security concerns. instead of links route perform operation have forms , buttons submit request put route change user status.

question: how generate such forms (buttons) based on list of users returned doctrine?

form code used create form/button in user profile:

    /**  * creates form activate/deactivate user entity id.  *  * @param mixed $id entity id  *  * @return \symfony\component\form\form form  */ private function createactivationdeactivationform($id) {     return $this->createformbuilder()         ->setaction($this->generateurl('user_activate', array('id' => $id)))         ->setmethod('put')         ->add('submit', 'submit', array('label' => 'activate/deactivate'))         ->getform()     ; }  

controller code used user profile:

    /**  * @route("/user/{id}", name="user_show")  * @method("get")  * @template()  */ public function showuseraction($id) {     $em = $this->getdoctrine()->getmanager();      $user = $em->getrepository('testuserbundle:user')->find($id);      if (!$user) {         throw $this->createnotfoundexception('unable find user');     }      $deleteform = $this->createdeleteform($id);                     $activateform = $this->createactivationdeactivationform($id);                      return array(         'user' => $user,         'delete_form' => $deleteform->createview(),         'activate_form' => $activateform->createview(),      ); } 

controller put method perform operation user profile:

    /**  * activate user.  *  * @route("/{id}", name="user_activate")  * @method("put")  */ public function activateaction(request $request, $id) {     $form = $this->createactivationdeactivationform($id);     $form->handlerequest($request);      if ($form->isvalid()) {         $em = $this->getdoctrine()->getmanager();         $user = $em->getrepository('testuserbundle:user')->find($id);          if (!$user) {             throw $this->createnotfoundexception('unable find user');         }          $current_user_activity_flag = $user->getactive();          $user->setactive(abs($current_user_activity_flag-1));          $em->persist($user);         $em->flush();     }      return $this->redirect($this->getrequest()->headers->get('referer')); }  

controller code used users list:

    /**  * @route("/users", name="users_list")  * @method("get")  * @template()  */ public function listusersaction() {     $em = $this->getdoctrine()->getmanager();      $users = $em->getrepository('testuserbundle:user')->findexistingusers();      //$deleteform = $this->createdeleteform($id);                     //$activateform = $this->createactivationdeactivationform($id);                      return array(         'users' => $users,         //'delete_form' => $deleteform->createview(),         //'activate_form' => $activateform->createview(),      ); } 

i can not pass id form did operation profile cause every user there different id , more of symfony generates first form , ignores rest.

any idea how handle it? or maybe approach form/buttons incorrect , should use links instead?

i found solution works though not sure if it's compliant best practices.

instead of passing 1 form object in controller generated array of them keys based on user id. when looping through array in twig template use user id refer form object created current user.

mentioned in question controller user listing should this:

    /**  * @route("/users", name="users_list")  * @method("get")  * @template()  */ public function listusersaction() {     $em = $this->getdoctrine()->getmanager();      $users = $em->getrepository('psuserbundle:user')->findexistingusers();      $activate_forms = array();     $delete_forms = array();      foreach($users $user)     {          $activate_forms[$user->getid()] = $this->createactivationdeactivationform($user->getid())->createview();         $delete_forms[$user->getid()] = $this->createdeleteform($user->getid())->createview();     }      return array(         'users' => $users,         'delete_forms' => $delete_forms,         'activate_forms' => $activate_forms,      ); } 

... , in twig form within foreach should refered this:

    {{ form_start(activate_forms[user.id], {'attr': {'novalidate': 'novalidate'}}) }}          {% if user.active %}             {{ form_widget(activate_forms[user.id].submit, {'attr': {'class': 'btn btn-xs btn-warning btn-block'}, 'label' : 'deactivate'}) }}         {% else %}             {{ form_widget(activate_forms[user.id].submit, {'attr': {'class': 'btn btn-xs btn-success btn-block'}, 'label' : 'activate'}) }}         {% endif %}        {{ form_end(activate_forms[user.id]) }}   

Comments