2013-10-29 61 views
0

我想更新用戶實體(用唯一用戶標識選擇)。使用輸入域(表單)更新顯示值

它應該看起來像一個表(寬度= 3字段),在左邊你有列名(名字,姓氏等),中間是當前值,右邊是新的輸入字段值。

那是在我edit.html.twig與div的成爲當前表:

<div> 
    <div> 
     <span></span> 
     <span>Current</span> 
     <span>Edit here</span> 
    </div> 
    <div> 
     <span>Emailadress:</span> 
     <span>{{ userById.getEmail }}</span> 
     <span></span> 
    </div> 
    <div> 
     <span>First name:</span> 
     <span>{{ userById.getFirstname }}</span> 
     <span></span> 
    </div> 
    <div> 
     <span>Last name:</span> 
     <span>{{ userById.getLastname }}</span> 
     <span></span> 
    </div> 
</div> 


現在問題來了。在最後一個跨度內,我需要輸入字段來編輯值。我嘗試使用填充實體值的表單並將其與名稱字段和當前字段組合在一起。

我嘗試一些Editfields插入Controllerfunction下面,沒得到它的工作:(

目前蔭選擇的用戶ID,建設形式有了它,值傳遞給我的編輯視圖。

只有inputfields丟失,顯示當前數據工作正常,下面是我的editfunction:?

public function editUserAction($id, Request $request) { 

    $em = $this->getDoctrine()->getManager(); 
    $userId = $em->getRepository('CompanyUserBundle:User')->find($request->get('id')); 


    $form = $this->createForm(new UserForm($request->getSession()), new User(), array(
      'action' => $this->generateUrl('backend_user_management_edit', array('id' => $userId->getId())), 
      'em' => $em) 
    ); 
    $form->handleRequest($request); 


    $userData = $this->getDoctrine() 
     ->getRepository('CompanyUserBundle:User') 
     ->findOneById($userId); 

    if (!$userData) { 
     throw $this->createNotFoundException(
      'No user found for id '.$userId 
     ); 
    } 

    return $this->render(
     'CompanyUserBundle:User:edit.html.twig', 
     array(
      'form' => $form->createView(), 
      'action' => $this->generateUrl('backend_user_management_edit', array('id' => $userId->getId())), 
      'userById' => $userId 
     ) 
    ); 
} 

我如何添加Inputfields編輯值,並用新值更新當前用戶

隨着親切的問候

+0

[閱讀文檔...](http://symfony.com/doc/current/book/forms。 html) – max

+0

偏題:你加載用戶兩次('find()'和'findOneById()')。你應該用'$ id'替換$ request-> get('id')',因爲'$ id'是你的函數的一個參數。 –

+0

非常感謝你的幫助 – amoht

回答

0

控制器

public function editUserAction($id, Request $request) { 

    $em = $this->getDoctrine()->getManager(); 
    $userId = $request->get('id'); 
    $user = $this->getDoctrine() 
     ->getRepository('CompanyUserBundle:User') 
     ->findOneById($userId); 

    if (!$userData) { 
     throw $this->createNotFoundException(
      'No user found for id '.$userId; 
     ); 
    } 

    $form = $this->createForm(new UserForm($request->getSession()), $user, array(
      'action' => $this->generateUrl('backend_user_management_edit', array('id' => $userId->getId())), 
      'em' => $em) 
    ); 
    $form->handleRequest($request); 

    return $this->render(
     'CompanyUserBundle:User:edit.html.twig', 
     array(
      'form' => $form->createView(), 
      'action' => $this->generateUrl('backend_user_management_edit', array('id' => $userId->getId())), 
      'userId' => $userId, 
      'user' => $user 
     ) 
    ); 
} 

查看

{{ form_start(form) }} 
<div> 
    <div> 
     <span></span> 
     <span>Current</span> 
     <span>Edit here</span> 
    </div> 
    <div> 
     <span>Emailadress:</span> 
     <span>{{ user.email }}</span> 
     <span>{{ form.email }}</span> 
    </div> 
    <div> 
     <span>First name:</span> 
     <span>{{ user.firstname }}</span> 
     <span>{{ form.firstname }}</span> 
    </div> 
    <div> 
     <span>Last name:</span> 
     <span>{{ user.getLastname }}</span> 
     <span>{{ form.lastname }}</span> 
    </div> 

    <input type="submit" /> 
</div> 
{{ form_end(form) }}  
+0

這樣的事情,我還沒有測試過。 – max

+0

感謝您的工作,我收到了一些錯誤,但生病試圖解決它,明天更多關於它 – amoht