Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Form;
- use App\Entity\Account;
- use Symfony\Component\Form\AbstractType;
- use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
- use Symfony\Component\Form\Extension\Core\Type\EmailType;
- 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\Form\FormEvent;
- use Symfony\Component\Form\FormEvents;
- use Symfony\Component\Form\FormInterface;
- use Symfony\Component\Form\FormView;
- use Symfony\Component\OptionsResolver\OptionsResolver;
- use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
- use Symfony\Component\Validator\Constraints\IsTrue;
- class AccountType extends AbstractType
- {
- public const DEFAULT_VALUE_PASS = 'password';
- private $passwordEncoder;
- public function __construct(UserPasswordEncoderInterface $passwordEncoder)
- {
- $this->passwordEncoder = $passwordEncoder;
- }
- public function buildForm(FormBuilderInterface $builder, array $options)
- {
- $builder
- ->add('login', TextType::class, [
- 'label' => 'label.login'
- ->add('plainPassword', RepeatedType::class, [
- 'type' => PasswordType::class,
- 'first_options' => ['label' => 'label.password'],
- 'second_options' => ['label' => 'label.password.repeat'],
- ])->add('coins', TextType::class, [
- 'label' => 'label.coins'
- ])->add('email', EmailType::class, [
- 'label' => 'label.email'
- ])->add('code', TextType::class, [
- 'label' => 'label.code',
- ])->addEventListener(FormEvents::PRE_SET_DATA, [
- $this, 'onPreSetData'
- ])->addEventListener(FormEvents::POST_SUBMIT, [
- $this, 'onPostSubmit'
- ]);
- }
- public function onPreSetData(FormEvent $event)
- {
- $form = $event->getForm();
- /** @var $user Account */
- $form = $form->getData();
- if ($user->getId()) {
- return;
- }
- $form->add('question', TextType::class, [
- 'label' => 'label.question'
- ])
- ->add('answer', TextType::class, [
- 'label' => 'label.answer'
- ])
- ->add('rules', CheckboxType::class, array(
- 'label' => 'label.terms.statement',
- 'mapped' => false,
- 'constraints' => new IsTrue([
- 'message' => 'terms'
- ]),
- 'required' => true
- ))
- ;
- }
- public function onPostSubmit(FormEvent $event)
- {
- /** @var $user Account */
- $user = $event->getForm()->getData();
- if ($user->getId() === null) {
- return;
- }
- if ($user->getPlainPassword() === self::DEFAULT_VALUE_PASS) {
- return;
- }
- $user->setPassword($this->passwordEncoder->encodePassword($user, $user->getPlainPassword()));
- }
- public function buildView(FormView $view, FormInterface $form, array $options)
- {
- $view->vars['default_value_pass'] = self::DEFAULT_VALUE_PASS;
- }
- public function configureOptions(OptionsResolver $resolver)
- {
- $resolver->setDefaults([
- 'data_class' => Account::class
- ]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement