A simple solution to your problem is to turn off the display of the password field and manually copy its value into your model if it is not empty. Code example:
$form = $this->createFormBuilder() ->add('name', 'text') ->add('email', 'repeated', array('type' => 'email')) ->add('password', 'repeated', array('type' => 'password', 'mapped' => false)) // ... ->getForm(); // Symfony 2.3+ $form->handleRequest($request); // Symfony < 2.3 if ('POST' === $request->getMethod()) { $form->bind($request); } // all versions if ($form->isValid()) { $user = $form->getData(); if (null !== $form->get('password')->getData()) { $user->setPassword($form->get('password')->getData()); } // persist $user }
You can also add this logic to your form type if you prefer to clean the controllers:
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormInterface $form) { $form = $event->getForm(); $user = $form->getData(); if (null !== $form->get('password')->getData()) { $user->setPassword($form->get('password')->getData()); } });
source share