简体   繁体   English

在Symfony2中编辑表单

[英]Edit form in Symfony2

I am not using Doctrine's generator because I think the code is ugly, thus I am trying to make it work my editAction and render the form in my edit.html.twig page, but I can not make it work. 我不使用Doctrine的生成器,因为我认为代码很丑陋,因此我试图使其在我的editAction并在我的edit.html.twig页面中呈现该表单,但是我无法使其起作用。 I tried to follow other examples given by other answers but they did not work either. 我尝试遵循其他答案给出的其他示例,但它们也不起作用。

First, my editAction . 首先,我的editAction

public function editAction($id, Request $request)
{
    $manager = $this->getDoctrine()->getManager();
    $entity = $manager->getRepository('PanelBundle:Material')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('O material procurado não está cadastrado');
    }

    $form = $this->createForm(new MaterialType());
    $form->handleRequest($request);

    if ($request->getMethod() == 'POST') {
        $form->submit($request);

        if ($form->isValid()) {
            $manager->merge($entity);
            $manager->flush();

            $this->addFlash('success', 'Material alterado');

            return $this->redirect($this->generateUrl('panel_materials'));
        }
    }

    return $this->render('PanelBundle:Material:edit.html.twig', array(
        'entity' => $entity
    ));
}

Now, my Twig 现在,我的树枝

{% extends '::layout.html.twig' %}

{% block title %}Editar {{ entity.name }}{% endblock %}

{% block body %}
    <div class="container">
        <h1>Editar {{ entity.name }}</h1>

        <form action="{{ path('panel_edit_material', { 'id': entity.id }) }}" method="POST">
            <div class="form-group">
                {{ form_row(entity.name) }}
            </div>
            <div class="form-group">
                {{ form_row(entity.description) }}
            </div>
            <div class="form-group">
                {{ form_row(entity.quantity) }}
            </div>
            <div class="form-group">
                {{ form_row(entity.price) }}
            </div>

            {{ form_rest(form) }}

            <button type="submit" class="btn btn-success">Alterar</button>
        </form>
    </div>
{% endblock %}

Two things: if I remove the { 'id': entity.id } , an error is shown saying a mandatory field is missing, if I let it like it is now, it says searchAndRenderBlock() must be an instance of Symfony\\Component\\Form\\FormView , and if instead of returning $entity in controller, and return $form->createView() , the error says FormView could not be converted to string 两件事:如果删除{ 'id': entity.id } ,则会显示错误{ 'id': entity.id } ,说明缺少必填字段;如果让它像现在这样显示,则表明searchAndRenderBlock() must be an instance of Symfony\\Component\\Form\\FormView ,如果不是在控制器中返回$entity并返回$form->createView() ,则错误提示FormView could not be converted to string

The FormView could not be converted to string error is probably caused when attempting to pass the entity id to the path function at { 'id': entity.id } . 尝试将实体id传递到{ 'id': entity.id }的路径函数时,可能导致FormView could not be converted to string错误。

The easiest solution to me seems to be to pass the form to the view (as you said you tried), but to remove the whole action="{{ path('panel_edit_material', { 'id': entity.id }) }}" attribute from the form tag. 对我来说,最简单的解决方案似乎是将表单传递给视图(就像您说的那样),但是要删除整个action="{{ path('panel_edit_material', { 'id': entity.id }) }}"从属性form的标记。

If the action attribute is missing, it will default to the current url (which should work if I understand the example). 如果缺少action属性,它将默认为当前网址(如果我理解该示例,该网址将起作用)。

If this does not work, you can still pass the id of the entity in this way: {{ path('panel_edit_material', { 'id': form.id.vars.value }) }} . 如果这不起作用,您仍然可以通过以下方式传递实体的ID: {{ path('panel_edit_material', { 'id': form.id.vars.value }) }} This also requires you to pass the form to the view instead of the Entity. 这还要求您将表单而不是实体传递给视图。

Hope this helps :) 希望这可以帮助 :)

Edit : upon re-reading, I noticed the use of {{ entity.name}} . 编辑 :重新阅读后,我注意到使用了{{ entity.name}} If you pass the for to the view, this will need to be changed to {{ form.name.vars.value }} , or you simply need to pass both the entity and the form to the view, so you can choose to display either the original value or the new value 如果将for传递给视图,则需要将其更改为{{ form.name.vars.value }} ,或者您只需要将entityform传递给视图,因此可以选择显示原始值或新值

I've managed to solve it thanks to the Brecht's enlightenment. 由于布雷希特(Brecht)的启发,我设法解决了这一问题。

The solution in my case was to remove the form_row(..) from the page and leave only like this: 在我的情况下,解决方案是从页面中删除form_row(..)并仅保留如下:

<form action="{{ path('panel_edit_material', { 'id': entity.id }) }}" method="POST">
    <div class="form-group">
        {{ form_rest(form) }}
    </div>
    <button type="submit" class="btn btn-success">Alterar</button>
</form>

In the Controller: 在控制器中:

$form = $this->createForm(new MaterialType(), $entity);

and: 和:

return $this->render('PanelBundle:Material:edit.html.twig', array(
    'form' => $form->createView(),
    'entity' => $entity
));

And that's it. 就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM