0

我想更新一个用户实体(使用唯一的用户 ID 选择)。

它应该看起来像一个表格(宽度 = 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>


现在问题来了。在最后一个跨度内,我希望输入字段来编辑值。我尝试使用一种用值填充实体并将其与名称字段和当前字段组合的表单。

我试图在下面的Controllerfunction中插入一些Editfields,但没有让它工作:(

目前我正在选择用户 ID 并用它构建一个表单,将值传递给我的编辑视图。

只有输入字段丢失,显示当前数据工作正常。下面是我的编辑功能:

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
        )
    );
}

如何添加输入字段来编辑值并使用新值更新当前用户?

亲切的问候

4

1 回答 1

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) }}    
于 2013-10-29T13:52:54.600 回答