Reputation: 1536
I'm building a symfony 3 project and I'm trying to show a flashbag message in twig template after submit the form to notice the user that the form has been submitted successfully but it's not working for me ! here is the controller :
<?php
namespace EvalBundle\Controller;
use EvalBundle\Entity\SessionEvaluation;
use EvalBundle\Form\SessionEvaluationType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class EvaluationController extends Controller
{
/**
* @Route("/evaluation", name="evaluation_session")
* @Method({"GET", "POST"})
*/
public function indexAction(Request $request)
{
$sessioneval = new SessionEvaluation();
$form = $this->createForm(SessionEvaluationType::class, $sessioneval);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$em = $this->getDoctrine()->getManager();
$em->persist($sessioneval);
$em->flush();
$this->get('session')->getFlashBag()->add('notice', 'la session est lancée avec succes');
unset($entity);
}
return $this->render('@Eval/Default/Evaluation/newEvaluationSession.html.twig',array(
'form' => $form->createView()
));
}
}
and here is the twig template :
{% extends ':default:superAdminBase.html.twig' %}
{% block menu %}
{% include(':default:menu.html.twig') %}
{% endblock %}
{% block header %}
{% endblock %}
{% block body %}
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<h1 class="page-header">
Lancer une nouvelle session d'évaluation
</h1>
</div>
</div>
{% set flashbag_notices = app.session.flashBag('notice') %}
{% if flashbag_notices is not empty %}
<div class="row">
<div class="col-lg-6">
<ul>
{% for notice in flashbag_notices %}
{{ dump(flashbag_notices) }}
<li>{{ notice }}</li>
{% endfor %}
</ul>
</div>
</div>
{% endif %}
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
Session d'évaluation des Compétences
</div>
<div class="panel-body">
<div class="row">
<div class="container">
<div class="col-lg-12 ">
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block javascript %}
{% endblock %}
I've followed the doc but I don't know it's not working on my side.
some help please ! thanks
Upvotes: 0
Views: 4080
Reputation: 7764
Try changing this line:
$this->get('session')->getFlashBag()->add('notice', 'la session est lancée avec succes');
TO:
$request->getSession()->getFlashBag()->add('notice', 'la session est lancée avec succes');
Alternately you can do:
$this->addFlash(
'notice',
'la session est lancée avec succes'
);
And in Twig get like so:
{% set flashbag_notices = app.session.flashBag('notice') %}
If you happen to be running Symfony 3.3, you can instead use this:
{% set flashbag_notices = app.flashes('notice') %}
As per the new changes:
https://symfony.com/blog/new-in-symfony-3-3-improved-flash-messages
Upvotes: 1