如何在模态上使用form_errors-在关闭模态并刷新页面之前显示错误

时间:2019-04-22 16:39:06

标签: symfony

在主页上提交模态表格时,我尝试显示错误 ,我尝试过这种方法,但这不是功能性的方法

       <div class="row">
          <ul>
             {% for error in formClasse.vars.errors.form.getErrors(true) %}
                <div class="col-12 alert alert-danger alert-dismissible fade show" role="alert">
                      <li>{{ error.message }}</li>
                      <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                           <span aria-hidden="true">&times;</span>
                      </button>
                </div>
             {% endfor %}
          </ul>
       </div>

<button class="float-right btn btn-success btn-rounded" data-toggle="modal" data-target="#ajouter_classe"><i class="fa fa-plus" data-backdrop="static" data-keyboard="false"></i> Ajouter une classe</button> 



 <!-- Start Modal ajouter Classe-->
    {{ include("@Admin/classe/new.html.twig") }}
 <!-- End Modal ajouter Classe -->

这是我们添加的主要模式

<div class="modal fade" id="ajouter_classe" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
{{ form_start(formClasse, { 'method' : 'post', 'action': path('admin_classe_new') }) }}
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <h4 class="modal-title">Ajouter une classe</h4>
            <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>

        <div class="modal-body">

            <div class="form-group custom-mt-form-group">
                {{ form_widget(formClasse.filiere) }}
                <label class="control-label">Filière - Niveau<span class="text-danger">*</span></label>
                <i class="bar"></i>
            </div>

            <div class="form-group custom-mt-form-group">
                {{ form_widget(formClasse.classe) }}
                <label class="control-label">Classe<span class="text-danger">*</span></label>
                <i class="bar"></i>
                {{ form_errors(formClasse.classe) }}
            </div>  

        </div>

        <div class="modal-footer">
            <button class="btn btn-primary mr-2" type="submit">Ajouter</button>
            <button class="btn btn-dark" data-dismiss="modal">Annuler</button>
        </div>
    </div>
</div>
{{ form_end(formClasse) }}

控制器

/**
 * Lists all cycle entities.
 *
 * @Route("/classes", name="admin_classes_index")
 * @Method("GET")
 */
public function indexClasseAction(Request $request)
{   

    $classe = new Classe();
    $em = $this->getDoctrine()->getManager();

    /*Classe forms*/
    $formClasse = $this->createForm('AdminBundle\Form\ClasseType', $classe);
    $editFormClasse = $this->createForm('AdminBundle\Form\ClasseType', $classe);

    $classes = $em->getRepository('AdminBundle:Classe')->findAll();


    return $this->render('@Admin/classe/index_gestion.html.twig', array(
        'classes' => $classes,
        'formClasse' => $formClasse->createView(),
        'edit_formClasse' => $editFormClasse->createView(),
    ));
}

/**
 * Creates a new classe entity.
 *
 * @Route("/classe/new", name="admin_classe_new")
 * @Method({"GET", "POST"})
 */
public function newClasseAction(Request $request)
{
    $classe = new Classe();
    $form = $this->createForm('AdminBundle\Form\ClasseType', $classe);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($classe);
        $em->flush();

        $this->addFlash('success', 'La nouvelle classe '.$classe->getClasse().' a été ajoutée avec succès');

        return $this->redirectToRoute('admin_classes_index');
    }

    return $this->redirectToRoute('admin_classes_index');
}

请提出任何建议,以避免标准方式(将页面添加到ADD,而不是按Modal进行添加),因为该想法是在同一页面上创建CRUD

0 个答案:

没有答案