0

After creating registration form in that way: https://symfony.com/doc/current/doctrine/registration_form.html There is a problem with creating account. In previous version I did it in the same way, and there weren't wny problem. I am not sure is it a bug or I do something in wrong way. Form is still invalid. In Profiler->Validation I can see a call: data.password This value should not be null.

I tried to remove data parameter from form buildier and remove Options Resolver and then works, but I know it's not a correct solution.

Removed from RegistrationFormType.php:

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => User::class,
    ]);
}

Changed in RegistrationController.php:

public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder, GuardAuthenticatorHandler $guardHandler, AppAuthenticator $authenticator): Response
{
    $user = new User();
    $form = $this->createForm(RegistrationFormType::class);
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $user->setEmail($form->get('email')->getData());
        $user->setPassword(
            $passwordEncoder->encodePassword(
                $user,
                $form->get('plainPassword')->getData()
            )
        );

Can you help me with correct solution of this problem?

k1000
  • 78
  • 1
  • 5
  • Setting nullable=true to password field in User entity works too when doing like way in documentation. – k1000 Jun 15 '19 at 19:45

1 Answers1

0

Symfony4 register-form error

Change all words: plainPassword to password in classes and twigs:

src/Form/RegistrationFormType.php
templates/register/register.html.twig
src/ControllerRegistrationController.php

And change to true or delete from (src/Form/RegistrationFormType.php):

'mapped' => true,
// or hide
// 'mapped' => false,

Or add plainPassword variable to src/Entity/User.php and do

# Update User class add plainPassword
php bin/console make:entity
> User
...

# in sqlite3
php bin/console doctrine:schema:update --force

# or in mysql
php bin/console make:migration
php bin/console doctrine:migrations:migrate

P.s php bin/console make:user don't create plainPassword (method and variable) but create register-form and template have 'plainPassword' variable (stupid symfony docs).

Regards Laick

Laick
  • 1
  • 1
  • It can works too, but still it's not answer why is it doesn't simply work with any ingerention. PlainPassword field is not mapped and I don't understand why form is invalid on this level with empty password value of user object. – k1000 Jun 18 '19 at 18:00