Last Updated: February 25, 2016
·
8.758K
· pleone

Sonata Admin Bundle User Password

To set a user password on create/update using Sonata Admin you need to:
+ Configure a preUpdate hook
+ Configure the service
+ Use the right filed type :)

Configuring preUpdate Hook
In your admin class (ex. UserAdmin) add this methods:

public function preUpdate($user)
    {
        $this->getUserManager()->updateCanonicalFields($user);
        $this->getUserManager()->updatePassword($user);
    }

    public function setUserManager(UserManagerInterface $userManager)
        {
            $this->userManager = $userManager;
        }

    public function getUserManager()
    {
            return $this->userManager;
    }

Configuring the Service

In your UserAdmin service you have to inject the UserManager (ex. FOS).
To do this, add this lines to your service declaration:

<call method="setUserManager">
          <argument type='service' id='fos_user.user_manager' />
      </call>

The right field type

For password you have to use a field named plainPassword to avoid password overwriting

protected function configureFormFields(FormMapper $formMapper)
        {
            $formMapper
                [..]
                ->add('plainPassword', 'password') 
                [...]
            ;
        }

1 Response
Add your response

Thanks for the tutorial, Just what I needed.
I add the configuration service in YAML format:
...
calls:
...
- [ setUserManager, ["@fosuser.usermanager"]]

over 1 year ago ·