Commit 106fd4c2 authored by Mat's avatar Mat

controller register, et UserType

parent 0d3c99c6
......@@ -2,7 +2,10 @@
namespace SocketBundle\Controller;
use SocketBundle\Entity\User;
use SocketBundle\Form\UserType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
......@@ -31,10 +34,39 @@ class ChatController extends Controller
{
$username = $utils->getLastUsername();
$error = $utils->getLastAuthenticationError();
return $this->render('@Socket/Login/login.html.twig', array(
'username' => $username,
'error' => $error
));
}
/**
* @Route(name="register", path="/register")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function registerAction(Request $request)
{
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$user->setRole(["ROLE_USER"]); // on affecte un rôle
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return $this->redirectToRoute('login');
}
return $this->render('@Socket/Login/register.html.twig', array(
'form' => $form->createView()
));
}
}
......@@ -3,6 +3,11 @@
namespace SocketBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
......@@ -14,11 +19,29 @@ class UserType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username')
->add('email')
->add('password')
->add('role')
->add('avatar');
->add('username', TextType::class, array())
->add('email', EmailType::class, array())
->add('password', RepeatedType::class, array(
'type' => PasswordType::class,
'first_options' => array( 'label' => "Password" ),
'second_options' => array( 'label' => "Confirm password" ),
'invalid_message' => "Password not matched !"
))
->add('file', FileType::class, array());
// 1)
// volontairement, on ne met pas de submit.
// Khun considère que ce n'est pas une bonne pratique.
// c'est génant dans le cas des formulaires imbriqués
// 2)
// formulaire imbriqué, on pourrait par exemple avoir
// ->add('message', MessageType::class)
// et gérer plusieurs entités dans le même formulaire
}/**
* {@inheritdoc}
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment