0

I have User Entity for user registration on Symfony 4

I have User entity which has password.

Now I want to update some parts of row by Sonata Admin Bundle.

However it fails to update User entity because of there is no password.

in my UserAdmin.php

$formMapper->add('email');
$formMapper->add('nickName',null,array('required' => false));
$formMapper->add('enabled',null,array('required' => false));

I just want to change nickName or email without touching password.

in Entity I don't use validation, use Assert like this

User.php

/**
 * @Assert\NotBlank()
 * @Assert\Length(max=4096)
 */
private $plainPassword;

/**
 * The below length depends on the "algorithm" you use for encoding
 * the password, but this works well with bcrypt.
 *
 * @ORM\Column(type="string", length=64)
 */
private $password;

When registration, it works well like this in Controller.

$password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
$user->setPassword($password);

$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($user);
$entityManager->flush();
whitebear
  • 11,200
  • 24
  • 114
  • 237
  • How exactly do you see that something is failing? Is there an error message given? – Nico Haase Dec 19 '18 at 13:26
  • It can get tricky once you start to basically split up entities. One reason I have moved away from the Doctrine ORM. Bit extreme I know. However, you probably want to take a look at [validation groups](https://symfony.com/doc/current/validation/groups.html). Groups will allow skipping the password validation stuff when necessary. I don't use Sonata so I can't give you the specifics on how to specify a group but I am sure it is in the docs. – Cerad Dec 19 '18 at 13:32
  • [Getting Started With FOSUserBundle](https://symfony.com/doc/current/bundles/FOSUserBundle/index.html) https://stackoverflow.com/questions/47844967/symfony-4-fosuserbundle > The Symfony Security component provides a flexible security framework > that allows you to load users from configuration, a database, or > anywhere else you can imagine. The FOSUserBundle builds on top of this > to make it quick and easy to store users in a database, as well as > functionality for registration, reset password and a profile page. – TsV Dec 19 '18 at 13:21
  • Are you sure you modified an existing User (iE loaded with `$repository->find($id)` and not created a new one? – Sebus Dec 19 '18 at 16:08
  • @TsV I have used FOSUserBundle for my Symfony3 project. However I found that there is some difficulity FOSUserBundle with Symfony4. So now I try to use another way. It is true that FOSUserBundle works well for this kind of things though ... – whitebear Dec 20 '18 at 18:38
  • @NicoHaase , I am sure the error is password validation. – whitebear Dec 20 '18 at 18:38
  • @Cerad. thanks split up is good alternative way. At first I try to understand validation groups. It's a great step forward. – whitebear Dec 20 '18 at 18:42
  • I am sure it doesn't create new one. – whitebear Dec 20 '18 at 18:42
  • For now, I remove `* @Assert\NotBlank()` from `/** * @Assert\NotBlank() * @Assert\Length(max=4096) */ private $plainPassword;` It might not good solution though. – whitebear Dec 21 '18 at 04:33

0 Answers0