简体   繁体   中英

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. I tried to follow other examples given by other answers but they did not work either.

First, my 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

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 } .

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.

If the action attribute is missing, it will default to the current url (which should work if I understand the example).

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

I've managed to solve it thanks to the Brecht's enlightenment.

The solution in my case was to remove the form_row(..) from the page and leave only like this:

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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