Advertisement
Guest User

Untitled

a guest
Aug 10th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.48 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Form;
  4.  
  5. use App\Entity\Account;
  6. use Symfony\Component\Form\AbstractType;
  7. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  8. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  9. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  10. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  11. use Symfony\Component\Form\Extension\Core\Type\TextType;
  12. use Symfony\Component\Form\FormBuilderInterface;
  13. use Symfony\Component\Form\FormEvent;
  14. use Symfony\Component\Form\FormEvents;
  15. use Symfony\Component\Form\FormInterface;
  16. use Symfony\Component\Form\FormView;
  17. use Symfony\Component\OptionsResolver\OptionsResolver;
  18. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  19. use Symfony\Component\Validator\Constraints\IsTrue;
  20.  
  21. class AccountType extends AbstractType
  22. {
  23.     public const DEFAULT_VALUE_PASS = 'password';
  24.  
  25.     private $passwordEncoder;
  26.  
  27.     public function __construct(UserPasswordEncoderInterface $passwordEncoder)
  28.     {
  29.         $this->passwordEncoder = $passwordEncoder;
  30.     }
  31.  
  32.     public function buildForm(FormBuilderInterface $builder, array $options)
  33.     {
  34.         $builder
  35.             ->add('login', TextType::class, [
  36.                 'label' => 'label.login'
  37.             ->add('plainPassword', RepeatedType::class, [
  38.                 'type' => PasswordType::class,
  39.                 'first_options' => ['label' => 'label.password'],
  40.                 'second_options' => ['label' => 'label.password.repeat'],
  41.             ])->add('coins', TextType::class, [
  42.                 'label' => 'label.coins'
  43.             ])->add('email', EmailType::class, [
  44.                 'label' => 'label.email'
  45.             ])->add('code', TextType::class, [
  46.                 'label' => 'label.code',
  47.             ])->addEventListener(FormEvents::PRE_SET_DATA, [
  48.                 $this, 'onPreSetData'
  49.             ])->addEventListener(FormEvents::POST_SUBMIT, [
  50.                 $this, 'onPostSubmit'
  51.             ]);
  52.     }
  53.  
  54.     public function onPreSetData(FormEvent $event)
  55.     {
  56.         $form = $event->getForm();
  57.         /** @var $user Account */
  58.         $form = $form->getData();
  59.  
  60.         if ($user->getId()) {
  61.             return;
  62.         }
  63.  
  64.         $form->add('question', TextType::class, [
  65.             'label' => 'label.question'
  66.         ])
  67.             ->add('answer', TextType::class, [
  68.                 'label' => 'label.answer'
  69.             ])
  70.             ->add('rules', CheckboxType::class, array(
  71.                 'label' => 'label.terms.statement',
  72.                 'mapped' => false,
  73.                 'constraints' => new IsTrue([
  74.                     'message' => 'terms'
  75.                 ]),
  76.                 'required' => true
  77.             ))
  78.         ;
  79.     }
  80.  
  81.     public function onPostSubmit(FormEvent $event)
  82.     {
  83.         /** @var $user Account */
  84.         $user = $event->getForm()->getData();
  85.  
  86.         if ($user->getId() === null) {
  87.             return;
  88.         }
  89.  
  90.         if ($user->getPlainPassword() === self::DEFAULT_VALUE_PASS) {
  91.             return;
  92.         }
  93.  
  94.         $user->setPassword($this->passwordEncoder->encodePassword($user, $user->getPlainPassword()));
  95.     }
  96.  
  97.     public function buildView(FormView $view, FormInterface $form, array $options)
  98.     {
  99.         $view->vars['default_value_pass'] = self::DEFAULT_VALUE_PASS;
  100.     }
  101.  
  102.     public function configureOptions(OptionsResolver $resolver)
  103.     {
  104.         $resolver->setDefaults([
  105.             'data_class' => Account::class
  106.         ]);
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement