Display flask messages

My flask messages don’t display on Symfony 6. I don’t know why. I’d like to display an error message when my user registers incorrectly (e-mail already taken, password and confirmation not identical, invalid username). In the logs, I can see that it works fine, I see that it doesn’t validate because password and confirmation are not identical when I test, but there’s no error message, which should be there thanks to “include”.

here my controller file :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><?php
namespace AppControllerSecurity;
use AppEntityUser;
use AppFormUserType;
use DoctrineORMEntityManagerInterface;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentPasswordHasherHasherUserPasswordHasherInterface;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyComponentValidatorValidatorValidatorInterface;
#[Route(name: 'app_security_')]
class RegisterController extends AbstractController{
#[Route('/register', name: 'register')]
public function register(Request $request, UserPasswordHasherInterface $passwordHasher, ValidatorInterface $validator, EntityManagerInterface $em) {
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if($form->isSubmitted()) {
$errors = $validator->validate($user);
if($errors->count() <= 0) {
$datetime = new DateTime('now');
$user->setRoles([])->setBanned(false)->setCreatedAt($datetime);
$user->setPassword($passwordHasher->hashPassword($user, $user->getPassword()));
$em->persist($user);
$em->flush();
$this->addFlash('success', "Félicitations, votre compte vient d'être créé !");
return $this->redirectToRoute('app_security_login');
} else {
foreach($errors as $violation) {
$this->addFlash('error', $violation->getMessage());
}
}
}
return $this->render('security/register.html.twig', [
'form' => $form->createView()
]);
}
}
</code>
<code><?php namespace AppControllerSecurity; use AppEntityUser; use AppFormUserType; use DoctrineORMEntityManagerInterface; use SymfonyBundleFrameworkBundleControllerAbstractController; use SymfonyComponentHttpFoundationRequest; use SymfonyComponentPasswordHasherHasherUserPasswordHasherInterface; use SymfonyComponentRoutingAnnotationRoute; use SymfonyComponentValidatorValidatorValidatorInterface; #[Route(name: 'app_security_')] class RegisterController extends AbstractController{ #[Route('/register', name: 'register')] public function register(Request $request, UserPasswordHasherInterface $passwordHasher, ValidatorInterface $validator, EntityManagerInterface $em) { $user = new User(); $form = $this->createForm(UserType::class, $user); $form->handleRequest($request); if($form->isSubmitted()) { $errors = $validator->validate($user); if($errors->count() <= 0) { $datetime = new DateTime('now'); $user->setRoles([])->setBanned(false)->setCreatedAt($datetime); $user->setPassword($passwordHasher->hashPassword($user, $user->getPassword())); $em->persist($user); $em->flush(); $this->addFlash('success', "Félicitations, votre compte vient d'être créé !"); return $this->redirectToRoute('app_security_login'); } else { foreach($errors as $violation) { $this->addFlash('error', $violation->getMessage()); } } } return $this->render('security/register.html.twig', [ 'form' => $form->createView() ]); } } </code>
<?php

namespace AppControllerSecurity;

use AppEntityUser;
use AppFormUserType;
use DoctrineORMEntityManagerInterface;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentPasswordHasherHasherUserPasswordHasherInterface;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyComponentValidatorValidatorValidatorInterface;

#[Route(name: 'app_security_')]
class RegisterController extends AbstractController{

  #[Route('/register', name: 'register')]
  public function register(Request $request, UserPasswordHasherInterface $passwordHasher, ValidatorInterface $validator, EntityManagerInterface $em) {

      $user = new User();
      $form = $this->createForm(UserType::class, $user);
      $form->handleRequest($request);

      if($form->isSubmitted()) {
        $errors = $validator->validate($user);
        if($errors->count() <= 0) {

            $datetime = new DateTime('now');
            $user->setRoles([])->setBanned(false)->setCreatedAt($datetime);
            $user->setPassword($passwordHasher->hashPassword($user, $user->getPassword()));

            $em->persist($user);
            $em->flush();

            $this->addFlash('success', "Félicitations, votre compte vient d'être créé !");
            return $this->redirectToRoute('app_security_login');
        } else {
          foreach($errors as $violation) {
            $this->addFlash('error', $violation->getMessage());
          }
        }
      }

      return $this->render('security/register.html.twig', [
          'form' => $form->createView()
      ]);
  }
}

here my template file which include another file :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{% extends 'base.html.twig' %}
{% block container %}
<h3>Twitya - Inscription</h3>
<p>Inscrivez-vous sur notre site Twitya.</p>
<hr class="mb-3">
<div class="row">
<div class="col-lg-3 col-md-2"></div>
<div class="col-lg-6 col-md-8">
{% include 'includes/_alerts.html.twig' %}
{{ form_start(form)}}
<div class="mb-3">
<label for="inputEmail" class="form-label">Adresse Email</label>
{{ form_widget(form.email) }}
<div id="emailHelp" class="form-text">Veuillez conserver votre adresse et pas la partager.</div>
</div>
<div class="mb-3">
<label for="inputPseudo" class="form-label">Pseudonyme</label>
{{ form_widget(form.username) }}
</div>
<div class="mb-3">
<label for="inputPassword" class="form-label">Mot de passe</label>
{{ form_widget(form.password) }}
</div>
<div class="mb-3">
<label for="inputPasswordConfirm" class="form-label">Confirmation du mot de passe</label>
{{ form_widget(form.passwordConfirm) }}
</div>
<button type="submit" class="btn btn-primary">S'inscrire</button>
{{ form_end(form)}}
</div>
<div class="col-lg-3 col-md-2"></div>
</div>
{% endblock %}
</code>
<code>{% extends 'base.html.twig' %} {% block container %} <h3>Twitya - Inscription</h3> <p>Inscrivez-vous sur notre site Twitya.</p> <hr class="mb-3"> <div class="row"> <div class="col-lg-3 col-md-2"></div> <div class="col-lg-6 col-md-8"> {% include 'includes/_alerts.html.twig' %} {{ form_start(form)}} <div class="mb-3"> <label for="inputEmail" class="form-label">Adresse Email</label> {{ form_widget(form.email) }} <div id="emailHelp" class="form-text">Veuillez conserver votre adresse et pas la partager.</div> </div> <div class="mb-3"> <label for="inputPseudo" class="form-label">Pseudonyme</label> {{ form_widget(form.username) }} </div> <div class="mb-3"> <label for="inputPassword" class="form-label">Mot de passe</label> {{ form_widget(form.password) }} </div> <div class="mb-3"> <label for="inputPasswordConfirm" class="form-label">Confirmation du mot de passe</label> {{ form_widget(form.passwordConfirm) }} </div> <button type="submit" class="btn btn-primary">S'inscrire</button> {{ form_end(form)}} </div> <div class="col-lg-3 col-md-2"></div> </div> {% endblock %} </code>
{% extends 'base.html.twig' %}

{% block container %}

  <h3>Twitya - Inscription</h3>
  <p>Inscrivez-vous sur notre site Twitya.</p>
  <hr class="mb-3">

  <div class="row">
      <div class="col-lg-3 col-md-2"></div>
      <div class="col-lg-6 col-md-8">

        {% include 'includes/_alerts.html.twig' %}

        {{ form_start(form)}}
        <div class="mb-3">
            <label for="inputEmail" class="form-label">Adresse Email</label>
            {{ form_widget(form.email) }}
            <div id="emailHelp" class="form-text">Veuillez conserver votre adresse et pas la partager.</div>
        </div>
        <div class="mb-3">
            <label for="inputPseudo" class="form-label">Pseudonyme</label>
            {{ form_widget(form.username) }}
        </div>
        <div class="mb-3">
            <label for="inputPassword" class="form-label">Mot de passe</label>
            {{ form_widget(form.password) }}
        </div>
        <div class="mb-3">
            <label for="inputPasswordConfirm" class="form-label">Confirmation du mot de passe</label>
            {{ form_widget(form.passwordConfirm) }}
        </div>
        <button type="submit" class="btn btn-primary">S'inscrire</button>
        {{ form_end(form)}}

      </div>
      <div class="col-lg-3 col-md-2"></div>
  </div>

{% endblock %}

and here my template flask messages which I include in my file template above :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{% for message in app.flashes('success') %}
<div class="alert alert-success" role="alert">
{{ message }}
</div>
{% endfor %}
{% for message in app.flashes('error') %}
<div class="alert alert-error" role="alert">
{{ message }}
</div>
{% endfor %}
</code>
<code>{% for message in app.flashes('success') %} <div class="alert alert-success" role="alert"> {{ message }} </div> {% endfor %} {% for message in app.flashes('error') %} <div class="alert alert-error" role="alert"> {{ message }} </div> {% endfor %} </code>
{% for message in app.flashes('success') %}
  <div class="alert alert-success" role="alert">
      {{ message }}
  </div>
{% endfor %}

{% for message in app.flashes('error') %}
  <div class="alert alert-error" role="alert">
      {{ message }}
  </div>
{% endfor %}

I didn’t find any solutions

New contributor

Olivier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật