提交编辑模式(Symfony)

时间:2018-01-02 14:03:46

标签: php symfony

其实我遇到了问题,但仍然无法找到合适的解决方案,这是我的问题:

我正在使用PHP Symfony开发一个应用程序,但是当我试图在模态上有一个编辑表单时,保存按钮不起作用(我可以在模态上获取数据,但模态不是操作),这是我的代码:

我在控制器上的方法:

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

      
      $em = $this->getDoctrine()->getManager();

    $etudiant = $em->getRepository('ECPlatformBundle:Etudiant')->find($id);

    if (null === $etudiant) {
      throw new NotFoundHttpException("L'étudiant d'id ".$id." n'existe pas.");
    }

  $form = $this->get('form.factory')->create(EtudiantType::class, $etudiant); 


    if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {

    
      // Inutile de persister ici, Doctrine connait déjà notre etudiant

        $em->flush();


      $request->getSession()->getFlashBag()->add('noticeEdit', 'Ligne bien modifiée.');

      return $this->redirectToRoute('ec_platform_etudiant');
       

     }
      
        return $this->render('ECPlatformBundle:Exam:editBisEtudiant.html.twig', array(
            'etudiant'      => $etudiant,
            'form'   => $form->createView(),

        ));
  


}

编辑视图:

{# src/OC/PlatformBundle/Resources/views/Advert/edit.html.twig #}
{% form_theme form 'bootstrap_3_layout.html.twig'  %}


 

<div class="bd-example">
        <button type="button" class="btn btn-primary" id="" data-toggle="modal" data-target="#exampleModal{{ etudiant.id }}" data-whatever="@getbootstrap">Modifier </button>
        <div class="modal fade" id="exampleModal{{ etudiant.id }}"  tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog" role="document">
                <br><br><br>
                <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" id="exampleModalLabel">Modifier Personne</h4>
                    </div>
                    <div class="modal-body">
    
                                 {{ form(form) }}

                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Annuler</button>
                       

                    </div>
                    

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

主视图(调用编辑视图并具有实体列表..):

{# src/EC/PlatformBundle/Resources/views/Exam/accueil.html.twig #}



{% extends "ECPlatformBundle::layout.html.twig" %}

{% block title %}
  Accueil - {{ parent() }}
{% endblock %}

{% block ecplatform_body %}

  

<p>Table des étudiants :</p>

<div class="container">
  <h2>Table</h2>
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>Nom</th>
        <th>Prénom</th>
        <th>CIN</th>
        <th>CNE</th>
        <th>date de naissance</th>
        <th>Niveau</th>
        <th>Email</th>
        <th>Action</th>
      </tr>
    </thead>

    <tbody>

      {% for etudiant in listEtudiants %}
      <tr>
        <td id="nom">{{ etudiant.nom }}</td>
        <td id="prenom">{{ etudiant.prenom }}</td>
        <td id="cin">{{ etudiant.cin }}</td>
        <td id="cne">{{ etudiant.cne }}</td>
        <td id="birthdate">{{ etudiant.birthdate|date('d-m-Y') }}</td>
        <td id="level">{{ etudiant.level }}</td>
        <td id="mail">{{ etudiant.mail }}</td>
        <td> <a href="{{ path('ec_platform_deletudiant', {'id': etudiant.id}) }}" class="btn btn-danger" id="deletebtn" onclick="return confirm('are u sure?')">
      
      Delete
      <i class="glyphicon glyphicon-trash"></i>
    </a>


 
  <!-- Modal -->
 
{{ render(controller("ECPlatformBundle:Exam:editBisEtudiant",{'id': etudiant.id })) }}

<!--Fin Modal !-->


<a href="{{ path('ec_platform_editetudiant', {'id': etudiant.id}) }}" type="button" class="btn btn-default"  id="modalshow">Update <i class="glyphicon glyphicon-edit"></i></a>


      </td>
      </tr>

    {% else %}

    <p>Pas d'étudiant trouvé!</p>
       {% endfor %}
     
    </tbody>
  </table>
</div>





{% endblock %}

请问我想知道为什么编辑方法在单独的视图上而不是在模态中工作..我可以在不使用Ajax的情况下使其工作吗?

非常感谢! :)

1 个答案:

答案 0 :(得分:0)

首先猜测您的提交按钮不在您的表单中。试试这个:

                <div class="modal-body">

                             {{ form_start(form) }}

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Annuler</button>
                             {{ form_end(form) }}


                </div>