src/Form/SignupType.php line 27

Open in your IDE?
  1. <?php
  2.  
  3. namespace App\Form;
  4.  
  5. use Symfony\Component\Form\FormError;
  6. use Symfony\Component\Form\FormEvent;
  7. use Symfony\Component\Form\FormEvents;
  8. use Boab\CmsBundle\Entity\User;
  9. use Symfony\Component\Form\AbstractType;
  10. use Symfony\Component\Form\FormBuilderInterface;
  11. use Symfony\Component\Validator\Constraints\Email;
  12. use Symfony\Component\Validator\Constraints\NotBlank;
  13. use Symfony\Component\OptionsResolver\OptionsResolver;
  14. use Symfony\Component\Form\Extension\Core\Type\TextType;
  15. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  16. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  17. use Symfony\Component\Form\Extension\Core\Type\CountryType;
  18. use Boab\CmsBundle\Repository\UserRepositoryInterface;
  19. use Boab\CmsBundle\Security\RandomGeneratorInterface;
  20. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  21. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  22. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  23. use Symfony\Component\Validator\Constraints\IsTrue;
  24. use Symfony\Component\Validator\Constraints\Length;
  25. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  26. class SignupType extends AbstractType
  27. {
  28.     protected $userRepository;
  29.     protected $authorizationChecker;  
  30.     private $passwordHasher;
  31.     private $randomStringGenerator;
  32.     public function __construct(
  33.         UserRepositoryInterface $userRepository
  34.         AuthorizationCheckerInterface $authorizationChecker,
  35.         UserPasswordHasherInterface $passwordHasher,
  36.         RandomGeneratorInterface $randomStringGenerator)
  37.     {
  38.         $this->userRepository $userRepository;
  39.         $this->authorizationChecker $authorizationChecker;
  40.         $this->passwordHasher $passwordHasher;
  41.         $this->randomStringGenerator $randomStringGenerator;
  42.     }      
  43.     
  44.     public function buildForm(FormBuilderInterface $builder, array $options)
  45.     {        
  46.         $builder
  47.             ->add('firstname'TextType::class, [     
  48.              
  49.                     'attr' => [
  50.                         'class'=>'form-control',
  51.                         'placeholder'=>'First Name',  
  52.                     ],
  53.                     'constraints' => [
  54.                         new NotBlank()
  55.                     ],                    
  56.                 ]
  57.             ) 
  58.             ->add('lastname'TextType::class, [   
  59.                      
  60.                     'attr' => [
  61.                         'class'=>'form-control',
  62.                         'placeholder'=>'Last Name'
  63.                     ],
  64.                     'constraints' => [
  65.                         new NotBlank()
  66.                     ],                    
  67.                 ]
  68.             )                                          
  69.             ->add('email'EmailType::class, [                    
  70.                     'attr' => [
  71.                         'class'=> 'form-control',
  72.                         'placeholder'=>'Email Address'
  73.                     ],
  74.                     'constraints' => [
  75.                         new NotBlank(array('message' => 'This field is require')),
  76.                         new Email(['message' => 'Invalid email address.'])                        
  77.                     ],
  78.                 ]
  79.             )
  80.             ->add('agreeTerms'CheckboxType::class, [
  81.                 'mapped' => false,
  82.                 'attr' => [
  83.                     'class' => 'custom-control-input'
  84.                 ],
  85.                 'constraints' => [
  86.                     new IsTrue([
  87.                         'message' => 'You should agree to our terms.',
  88.                     ]),
  89.                 ],
  90.             ]) 
  91.             ->add('plainPassword'PasswordType::class, [
  92.                 // instead of being set onto the object directly,
  93.                 // this is read and encoded in the controller
  94.                 
  95.                 'mapped' => false,
  96.                 'attr' => [
  97.                     'autocomplete' => 'new-password'
  98.                     'class' => 'form-control',
  99.                     'placeholder'=>'Password'
  100.                 ],
  101.                 'constraints' => [
  102.                     new NotBlank([
  103.                         'message' => 'Please enter a password',
  104.                     ]),
  105.                     new Length([
  106.                         'min' => 6,
  107.                         'minMessage' => 'Your password should be at least {{ limit }} characters',
  108.                         // max length allowed by Symfony for security reasons
  109.                         'max' => 4096,
  110.                     ]),
  111.                 ],
  112.             ])                       
  113.         ;
  114.         $builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event){
  115.             $form $event->getForm();
  116.             $customer $event->getData();
  117.             $username $this->randomStringGenerator->generate(6);
  118.             if(!$customer->getId()){
  119.                 $passwordEncoded $this->passwordHasher->hashPassword($customer$form->get('plainPassword')->getData());
  120.                 $customer->setPassword($passwordEncoded);  
  121.                 $customer->setUsername(strtolower($username));
  122.                 $customer->setIsActivated(false);
  123.                 $customer->setCreatedAt(new \DateTime('now'));
  124.             }
  125.         });
  126.         $builder->get('email')->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event)use($options){
  127.             $email $event->getData();
  128.             $user $this->userRepository->findOneBy(['email'=>$email]);
  129.             if($user){
  130.                 $event->getForm()->addError(new FormError('A user with this email already exists'));
  131.             }
  132.         }); 
  133.     }
  134.     public function configureOptions(OptionsResolver $resolver)
  135.     {
  136.         $resolver->setDefaults([
  137.             'data_class' => User::class,
  138.             'user'=>null,
  139.         ]);
  140.     }     
  141.      
  142. }