Sirius
Sirius

Reputation: 653

Open a modal with dynamic value [symfony2]

I have a page that display the user products, a user can edit a product. If the the user click on the button edit he will be redirect to the edit page : http://127.0.0.1/symfony/web/app_dev.php/post/125 I would like to display the edit form in a modal so the user will not be redirect.

I have the following error:

An exception has been thrown during the rendering of a template ("The identifier id is missing for a query of Xxx\XxxxxBundle\Entity\Post") in src\Xxx\XxxxxBundle\Resources\views\Post\index.html.twig at line 404.

My link was like this:

<div class="action">
<a href="{{ path('post_edit', { 'id': entity.id }) }}" class="buttonModal button btn-small full-width">EDIT</a>                                                                                       
</div>

I changed it to:

<div class="action">
<a href="#" class="buttonModal button btn-small full-width">EDIT</a>                                                                                       
</div>

This is how I render my controller in my modal:

<div class="container">
    <div class="row">
        <div class="cd-user-modal">
            <div class="cd-user-modal-container"> 
                <div id="cd-signup">
                    <div class="col-sm-12 col-sm-offset-2 form-box">
                        <div class="form-bottom">  

                      {{render(controller('FLYBookingsBundle:Post:edit')) }}

                        </div>
                    </div> 
                </div> 
            </div> 
        </div>
    </div>
</div>

This is the edit form:

<form action="{{ path('post_update', { 'id': entity.id }) }}" method="post" {{ form_enctype(edit_form) }}>
    {{ form_errors(edit_form) }}
    <div class="row">

        <div class="col-md-3">

            <div class="form-group form-group-lg form-group-icon-left">
                <i class="fa fa-phone input-icon"></i>
                <label>Phone number</label>
                {{ form_widget(edit_form.telephone, {'type':'phone' ,'attr': {'id': 'phone','class': 'form-control phone',} }) }} {{ form_errors(edit_form.telephone) }}
            </div>
        </div>
        <div class="col-md-7">
            <div class="form-group form-group-lg form-group-icon-left">
                <i class="fa fa-map-marker input-icon"></i>
                <label>Address</label>
                {{ form_widget(edit_form.address, { 'attr': {'class': 'form-control',} }) }} {{ form_errors(edit_form.address) }}
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12 col-md-3">
            {{ form(edit_form) }}
        </div>
    </div>
</form>

Controller of listing page:

public function indexAction($user)
{
    $user = $this->container->get('security.token_storage')->getToken()->getUser();
    $em = $this->getDoctrine()->getManager();

    $findEntities = $em->getRepository('FLYBookingsBundle:Post')->findByBusId($user);
    $entities = $this->get('knp_paginator')->paginate($findEntities, $this->get('request')->query->get('page', 1), 5
    );

    return array(
        'entities' => $entities,
        'user' => $user,
    );

}

Edit controller:

/**
 * Displays a form to edit an existing Post entity.
 *
 * @Route("/{id}/edit", name="post_edit")
 * @Method("GET")
 * @Template()
 */
public function editAction($id)
{
    $em = $this->getDoctrine()->getManager();
    $user = $this->getUser();
    $this->getDoctrine()->getManager()->getRepository('FLYBookingsBundle:Post')->findBy(array('user' => $user));
    $entity = $em->getRepository('FLYBookingsBundle:Post')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Post entity.');
    }


    $editForm = $this->createEditForm($entity);

    return array(
        'entity' => $entity,
        'edit_form' => $editForm->createView(),
    );
}

Upvotes: 1

Views: 3653

Answers (1)

hounded
hounded

Reputation: 726

Quick browse I would guess the error gives a clue, you are rendering your edit controller that has a parameter /{id}/edit, which you are not passing it

{{ render(controller('FLYBookingsBundle:Post:edit', {}, { 'id': id, 'active': true } )) }}

check symfony2 render controller with get parameters

Ok i'm not to sure of your implementation so I'm just going to show you one I did with no code prettying or standardizing.

My modals

{% for meat in meats %}
<div class="modal fade" id="{{ meat[0] }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">
                {% if waitingProduct[meat[0]]|length > 0 %}
                    {{ waitingProduct[meat[0]][0].itemdescription }}
                {% else %}
                    No Product available, please contact stores
                {% endif %}
            </h4>
        </div>
        <form action="{{ path('formulation_change') }}" method="post">
        <div class="modal-body">
            {% for product in waitingProduct[meat[0]] %}
            <input type="radio" name="pallet" value="{{ product.id }}" > {{ product.typenumber }}
            <br />
            {% endfor %}
            <input type="hidden" name="recipe" value="{{ recipe }}" >
            {% if waitingProduct[meat[0]]|length > 0 %}
            <input type="hidden" name="itemCode" value="{{ waitingProduct[meat[0]][0].itemcode }}" >
            {% endif %}
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
            <button type="submit" class="btn btn-primary">Set Active</button>
        </div>
        </form>
    </div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div> 
{% endfor %}

the button to toggle the separate modals

 {% for meat in meats %}
    <td>
        <div class="btn btn-sm btn-success" data-toggle="modal" data-target="#{{ meat[0] }}"  id="map">Set Active</div>
    </td>
{% endfor %}

Upvotes: 1

Related Questions