I have an issue on my Symfony web code. I'm a beginner and I would make a simple registration form so I generated this form with the following command :"make:registration-form" after create a user entity (as describe on the symfony documentation). My registrationController file works fine and I can create users through the register twig template. So, I know that I have no problem to send values on my database. My issue is I need to create my form on my main template (not in the register template that I created just for the test). My problem is when I submit my form, there is no values send to my database. I see no error when I submit and I don't understant why on my register template, I can send my form on my data-base and it won't works on my main template.
This is my main template :
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="{{ asset('build/css/app.css') }}">
<link rel="stylesheet" href="{{ asset('build/css/app-login.css') }}">
<link rel="stylesheet" href="{{ asset('build/css/transition.css') }}">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha512-DTOQO9RWCH3ppGqcWaEA1BIZOC6xxalwEsw9c2QQeAIftl+Vegovlnee1c9QX4TctnWMn13TZye+giMm8e2LwA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
{% block body %}
<header>
<nav>
<a href="{{ path('main_index') }}"><img src="img/logo.png" alt="Idyl" class="logo"></a>
<ul>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="{{ path('app_devtool') }}"><font color="#ff8a00">Devtools</font></a>
</li>
<li class="search">
<a href="{{ path('lucky_number') }}">
<i class="fa-solid fa-magnifying-glass"></i>
</a>
</li>
{% if app.user %}
<li class="pseudo">
<a href="{{ path('app_user') }}"><font color="#00ffb6">{{ app.user.username }}</font></a>
</li>
{% else %}
<div class="btn-log">
<li>
<a href="#" class="btnLogin-popup">Login</a>
</li>
</div>
{% endif %}
<li class="hamburger">
<a href="{{ path('app_logout') }}">
<div class="bar"></div>
</a>
</li>
</ul>
</nav>
</header>
<main>
<!-- Login -->
<center>
<div class="wrapper active">
<span class="icon-close">
<ion-icon name="close"></ion-icon>
</span>
<div class="form-box log">
<h2>Login</h2>
<!-- <img src="img/design1/jarrex-mini.png" alt="Idyl" class="logo"> -->
<form method="post">
<div class="input-inbox">
<span class="icon"><ion-icon
name="person-circle"></ion-icon>
</span>
<input type="text" name="username" id="inputUsername" class="form-control" autocomplete="username" required>
<label for="inputUsername">Username</label>
</div>
<div class="input-inbox">
<span class="icon"><ion-icon
name="lock-closed"></ion-icon>
</span>
<input type="password" name="password" id="inputPassword" class="form-control" autocomplete="current-password" required>
<label for="inputPassword">Password</label>
<input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}">
</div>
<div class="remember-forgot">
<label><input type="checkbox"><font color="#fff"> Remember me</font></label>
<a href="#">Forgot Password</a>
</div>
<button type="submit" class="btn">Login</button>
<div class="log-register">
<p>Don't have an account? <a href="#" class="register-link"><font color="#fff">Register</font></a></p>
</div>
</form>
</div>
<!-- Registration -->
<div class="form-box register">
<h2>Registration</h2>
<form method="post">
<div class="input-inbox">
<span class="icon"><ion-icon
name="log-in-outline"></ion-icon>
</span>
<input name="{{ field_name(registrationForm.username) }}"
value="{{ field_value(registrationForm.username) }}"
class="form-control" type="text" required>
<label>Username</label>
</div>
<div class="input-inbox">
<span class="icon"><ion-icon
name="lock-closed"></ion-icon>
</span>
<input name="{{ field_name(registrationForm.plainPassword) }}"
value="{{ field_value(registrationForm.plainPassword) }}"
class="form-control" type="password" required>
<label>Password</label>
</div>
<div class="remember-forgot">
<label><input type="checkbox"><font color="#fff"> agree to the terms & conditions</font></label>
</div>
<button type="submit" class="btn">Register</button>
<div class="log-register">
<p>Already have an account? <a href="#" class="login-link"><font color="#fff">Login</font></a></p>
</div>
</form>
</div>
</div>
</center>
<!-- Page principale -->
</div>
<div class="vignette"></div>
<img src="img/main/work01/bckimg.png" data-speedx="0.1" data-speedy="0.01"
data-speedz="0"
data-distance="-200" class="parallax bg-img">
<img src="img/main/work01/02.png" data-speedx="0.05" data-speedy="0.32"
data-speedz="0.15"
data-distance="850" class="parallax pht02">
<img src="img/main/work01/03.png" data-speedx="0.1" data-speedy="0.125"
data-speedz="0.05"
data-distance="1400" class="parallax pht03">
<a href="#"><img src="img/main/work01/04.png" data-speedx="0.05" data-speedy="0.05"
data-speedz="3"
data-distance="700" class="parallax pht04"></a>
<img src="img/main/work01/05.png" data-speedx="0.07" data-speedy="0.05"
data-speedz="3"
data-distance="2600" class="parallax pht05">
<img src="img/main/work01/06.png" data-speedx="0.1" data-speedy="0.1"
data-speedz="4"
data-distance="2500" class="parallax pht06">
<img src="img/main/work01/07.png" data-speedx="0.2" data-speedy="0.112"
data-speedz="0.2"
data-distance="4000" class="parallax pht07">
</main>
<!-- 2ème page -->
<div class="block-1">
This is a test
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
<br/><br/><br/>
<center><p>Pseudo: {{ app.user.username }}</p></center>
{% endif %}
<br/><br/><br/>
</div>
<script src="{{ asset('build/css/app.js') }}"></script>
<script src="{{ asset('build/css/dsgn1.js') }}"></script>
<script type="module" src="https://unpkg.com/[email protected]/dist/ionicons/ionicons.esm.js"></script>
<script nomodule src="https://unpkg.com/[email protected]/dist/ionicons/ionicons.js"></script>
{% endblock %}
This is my MainController :
<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\RegistrationFormType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
class MainController extends AbstractController
{
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response
{
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$user->setPassword(
$userPasswordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
);
$entityManager->persist($user);
$entityManager->flush();
// do anything else you need here, like send an email
return $this->redirectToRoute('main_index');
}
return $this->render('main/main.html.twig', [
'registrationForm' => $form->createView(),
]);
}
}
This is my route.yaml file :
`
controllers:
resource:
path: ../src/Controller/
namespace: App\Controller
type: attribute
utilisateurs_index:
path: /utilisateurs
# the controller value has the format 'controller_class::method_name'
controller: App\Controller\UtilisateursController::index
lucky_number:
path: lucky/number
# the controller value has the format 'controller_class::method_name'
controller: App\Controller\LuckyController::number
blog_show:
path: /blog/{id}
# the controller value has the format 'controller_class::method_name'
controller: App\Controller\BlogController::show
main_index:
path: /
# the controller value has the format 'controller_class::method_name'
controller: App\Controller\MainController::register
app_register:
path: /register
controller: App\Controller\RegistrationController::register
app_login:
path: /login
controller: App\Controller\SecurityController::login
app_user:
path: /utilisateurs
controller: App\Controller\UtilisateursController::index
app_devtool:
path: /devtool
controller: App\Controller\DevtoolController::index`
This my registrationFormType file :
<?php
namespace App\Form;
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
class RegistrationFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('username')
->add('agreeTerms', CheckboxType::class, [
'mapped' => false,
'constraints' => [
new IsTrue([
'message' => 'You should agree to our terms.',
]),
],
])
->add('plainPassword', PasswordType::class, [
// instead of being set onto the object directly,
// this is read and encoded in the controller
'mapped' => false,
'attr' => ['autocomplete' => 'new-password'],
'constraints' => [
new NotBlank([
'message' => 'Please enter a password',
]),
new Length([
'min' => 6,
'minMessage' => 'Your password should be at least {{ limit }} characters',
// max length allowed by Symfony for security reasons
'max' => 4096,
]),
],
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}
Can you help me ?
I want to use my main template to send my registration form to my database.